!************************************************************************************
!Program to compute the likelihood function 
!i) X: parameter vector
!2) F: likelihood function to be passed to the amoeba subroutine
!************************************************************************************
SUBROUTINE likelihood_function(X,F,loglike)


USE mod_types
USE mod_param_initial
USE mod_parameters
USE mod_grid
USE mod_classerror
USE mod_update
USE mod_values
USE mod_wage



IMPLICIT NONE


!------------------------------
!(1) Value function variables
!------------------------------ 
INTEGER:: phi_1_grid_point, phi_1_grid_point_max, phi_2_grid_point, age1
INTEGER, DIMENSION (phi_grid_size):: task
REAL(dp):: phi_1_star, phi_2_star, phi_3_star
!REAL(dp), DIMENSION(:,:), ALLOCATABLE:: V_15, V_25, V_35, V_14, V_24, V_34, V_13, V_23, &
!                            V_33, V_12, V_22, V_32, V_11, V_21, V_31, V_16, V_26, V_36, V_17, V_27, V_37
!REAL(dp), DIMENSION(:), ALLOCATABLE:: value8, V_0, V_18, V_28, V_38

REAL(dp), DIMENSION(phi_grid_size):: V_18, V_28, V_38, V_0, value8

REAL(dp), DIMENSION(phi_grid_size ,3,3,3,3):: V_17, V_27, V_37

REAL(dp), DIMENSION(phi_grid_size ,3,3,3):: V_16, V_26, V_36

REAL(dp), DIMENSION(phi_grid_size ,3,3):: V_15, V_25, V_35

REAL(dp), DIMENSION(phi_grid_size ,3):: V_14, V_24, V_34

REAL(dp), DIMENSION(phi_grid_size ):: V_13, V_23, V_33

REAL(dp), DIMENSION(phi_grid_size ):: V_12, V_22, V_32

REAL(dp), DIMENSION(phi_grid_size ):: V_11, V_21, V_31

REAL(dp) :: z

INTEGER:: leveldummy3, leveldummy4, leveldummy5, leveldummy6

REAL(dp), DIMENSION (phi_grid_size,totsample):: value1, value2, value3, value4, value5, value6, value7
REAL(dp):: vcontmore
!REAL(dp):: maxdiff_v, maxdiff_v1, maxdiff_v2, maxdiff_v3, v1contmore, v2contmore, v3contmore
REAL(dp), DIMENSION (phi_grid_size):: vcont
REAL(dp), PARAMETER:: vpen= 0.0_dp
!REAL(dp), DIMENSION (phi_grid_size):: v1cont, v2cont, v3cont
!REAL(dp), DIMENSION(:,:), ALLOCATABLE:: value_op

!------------------------------------
!(2) Likelihood and data variables
!------------------------------------
!INTEGER, PARAMETER:: totsample = 1426
INTEGER, DIMENSION(109):: ind

! tiny = 1.0d-35
REAL(dp), PARAMETER:: tiny = 1.0d-20, tiny1=1.0d-3, tiny5 = 1.0d-5, tiny35 = 1.0d-35
!INTEGER:: t_temp
!parameter vector
REAL(dp), DIMENSION(num_param):: X
!INTEGER:: t_obs, outcome_true 
REAL(dp):: V_lik(10), V_0_lik(10), V_1_lik(10), V_2_lik(10), V_3_lik(10),  F(1)
!REAL(dp):: S0, S1,  S2, S3, SUMV
!array containing the accumulating probability of the sequence of observed choices for each individual from 1 to CAP_N
REAL(dp), DIMENSION(:,:,:), ALLOCATABLE:: like_prior_type
REAL(dp), DIMENSION(:,:), ALLOCATABLE:: like_prior
REAL(dp), DIMENSION(:), ALLOCATABLE:: wtd_like_ind, wtd_like_ind_check, wtd_like_ind_norm, neglike
REAL(dp), DIMENSION(totsample):: loglike
!REAL(dp), DIMENSION(:), ALLOCATABLE::like_ind
!REAL(dp), DIMENSION (CAP_N,CAP_TAU):: probtau
INTEGER, DIMENSION(10):: ind_perf_miss, ind_exit
REAL(dp):: prob_level_1, prob_level_2,&
		   prob_level_3, prob_level_4, prob_level_5, prob_level_6, &
		   prob_level_7, prob_level_8, prob_level_9, prob_level_10
REAL(dp):: prob_wage_1, prob_wage_2, prob_wage_3, prob_wage_4, prob_wage_5,&
           prob_wage_6, prob_wage_7, prob_wage_8, prob_wage_9, prob_wage_10
!REAL(dp):: prob_level, prob_level_temp
!REAL(dp):: new_prior_sum, adj_new_prior_sum, adj_prior_sum, prior_sum
REAL(dp), DIMENSION(2):: prob_perf_1, prob_perf_2, prob_perf_3,&
						 prob_perf_4, prob_perf_5, prob_perf_6, prob_perf_7, &
						 prob_perf_8, prob_perf_9, prob_perf_10 

!REAL(dp), DIMENSION(2):: prob_perf		  
INTEGER, DIMENSION(:,:), ALLOCATABLE:: level_o, perf_o
INTEGER, DIMENSION(:,:), ALLOCATABLE:: sal_o
INTEGER, DIMENSION(:), ALLOCATABLE:: edu, age, year
REAL(dp):: avg_pop_edu
INTEGER, DIMENSION(:), ALLOCATABLE:: avg_edu
INTEGER, DIMENSION(:), ALLOCATABLE:: totnum
REAL(dp):: minlike, mineglike
INTEGER:: minlikeloc(1), mineglikeloc(1)
INTEGER:: pred_task, tten

INTEGER, DIMENSION(totsample):: ind_delete
INTEGER, DIMENSION(1426):: delete_stata
INTEGER, DIMENSION(:), ALLOCATABLE:: tenure_2_L1, tenure_2_L2, tenure_3_L1, tenure_3_L2, tenure_3_L3, &
                                     tenure_4_L1, tenure_4_L2, tenure_4_L3, tenure_5_L1, tenure_5_L2, tenure_5_L3, &
									 tenure_6_L1, tenure_6_L2, tenure_6_L3, tenure_7_L1, tenure_7_L2, tenure_7_L3
		   

!---------------------
!(3) Belief variables
!---------------------
INTEGER, PARAMETER:: typesno=4, tot_tenure = 7
!INTEGER:: prior_grid_size, prior_grid_size_new
!REAL(dp):: priorsum
!REAL(dp):: sup_beta, upper_prior
!REAL(dp):: lower_prior, inf_beta
INTEGER:: lower_grid_point, upper_grid_point, prior_sim_dimension, prior_dimension
REAL(dp):: minval_prior, maxval_prior, p_4
!INTEGER:: minval_grid_point, maxval_grid_point
!INTEGER, DIMENSION(:), ALLOCATABLE:: ind_prior_temp
REAL(dp):: bupdate, bupdate2, bupdate3, bupdate4, bupdate5, bupdate6, bupdate7, bupdate8, bupdate9, bupdate10
!INTEGER, DIMENSION(:), ALLOCATABLE:: prior_grid_point
!INTEGER, DIMENSION(:),ALLOCATABLE:: ind_prior
!REAL(dp), DIMENSION(:), ALLOCATABLE:: prior_bis
INTEGER, PARAMETER:: prior_shock_dim=250
REAL(dp), DIMENSION(:,:), ALLOCATABLE:: prior
INTEGER, DIMENSION(:), ALLOCATABLE:: year0, year1, year2, year3, year4, year5, &
                                     year6, year7, year8,year9
REAL(dp), DIMENSION(:), ALLOCATABLE:: avg_prior
REAL(dp):: avg_pop_prior

REAL(dp):: pr1, pr2, pr3, pr4



!-------------------
!(4) Miscellaneous
!-------------------
INTEGER:: i1,i2,  i3, i66, i9, p1,p2,p3,p4,p5,p6,p7,p8,p9, p10, &
          p1_bis, p2_bis, p3_bis, p4_bis, p5_bis, p6_bis, p7_bis, p8_bis, p9_bis, p10_bis
INTEGER:: itenure
!INTEGER:: itenure_miss, itenure_exit 
!INTEGER:: ind_level_perf_missing
!INTEGER:: i15, i11, i10, i13, i12, i14, i22, i8, i4, i5, i6, i7, j1, j111



!******************************************************************************
!(1) Assign values to the parameters and retrieve alternative-specific values
!******************************************************************************
!--------------------------------------------
!a. Contained in the module mod_parameters
!--------------------------------------------
CALL parameter_generation(X)


!-------------------------------------------------
!b.  Record the actual values of the parameters
!-------------------------------------------------
!---------------------------------------------
!i. Keep track of the current param vector
!---------------------------------------------
!OPEN(unit=3100, file ='track_actual_param.txt', position="append")

WRITE(3100,*) "Parameter Values: "

WRITE(3100,*) ""
WRITE(3100,*) alpha_1
WRITE(3100,*) beta_1
WRITE(3100,*) alpha_2
WRITE(3100,*) beta_2
WRITE(3100,*) alpha_3
WRITE(3100,*) beta_3
WRITE(3100,*) xi_1
WRITE(3100,*) xi_2
WRITE(3100,*) xi_3
WRITE(3100,*) p_1
WRITE(3100,*) gammap_01
WRITE(3100,*) gammap_11
WRITE(3100,*) gammap_02
WRITE(3100,*) gammapast11_0
WRITE(3100,*) gammap_03
WRITE(3100,*) s_0
WRITE(3100,*) s_1
WRITE(3100,*) s_2
WRITE(3100,*) sigma1_u
WRITE(3100,*) sigma_eps
WRITE(3100,*) s1_5
WRITE(3100,*) s_3
WRITE(3100,*) s_002
WRITE(3100,*) s_003
WRITE(3100,*) sigma2_u
WRITE(3100,*) sigma3_u
WRITE(3100,*) w_74
WRITE(3100,*) w_75
WRITE(3100,*) w_76
WRITE(3100,*) w_77
WRITE(3100,*) w_78
WRITE(3100,*) w_79
WRITE(3100,*) d2
WRITE(3100,*) d4
WRITE(3100,*) gammap_04
WRITE(3100,*) s1_52
WRITE(3100,*) s1_53
WRITE(3100,*) s1_54
WRITE(3100,*) d6
WRITE(3100,*) d9
WRITE(3100,*) d10
WRITE(3100,*) d11
WRITE(3100,*) xi_24
WRITE(3100,*) sigma32_u
WRITE(3100,*) sigma33_u
WRITE(3100,*) sigma34_u
WRITE(3100,*) d7
WRITE(3100,*) d8
WRITE(3100,*) d5
WRITE(3100,*) d1
WRITE(3100,*) xi_26
WRITE(3100,*) phk2
WRITE(3100,*) phk3
WRITE(3100,*) s_4
WRITE(3100,*) d12
WRITE(3100,*) xi_32
WRITE(3100,*) xi_11
WRITE(3100,*) xi_14
WRITE(3100,*) p_2
WRITE(3100,*) p_3
WRITE(3100,*) xi_34
WRITE(3100,*) xi_15
WRITE(3100,*) d13
WRITE(3100,*) d14
WRITE(3100,*) d15
WRITE(3100,*) d
WRITE(3100,*) d16
WRITE(3100,*) d17
WRITE(3100,*) xi_21
WRITE(3100,*) d1_1
WRITE(3100,*) d1_2
WRITE(3100,*) d1_3
WRITE(3100,*) cpen
WRITE(3100,*) gammapast11_1
WRITE(3100,*) xi_25
WRITE(3100,*) xi_33
WRITE(3100,*) sigma22_u
WRITE(3100,*) sigma23_u
WRITE(3100,*) sigma24_u
WRITE(3100,*) s_0022 
WRITE(3100,*) s_0023 
WRITE(3100,*) s_0024 
WRITE(3100,*) s_0032 
WRITE(3100,*) s_0033 
WRITE(3100,*) s_0034 
WRITE(3100,*) sigma12_u
WRITE(3100,*) sigma13_u
WRITE(3100,*) sigma14_u
WRITE(3100,*) gammapast33_0
WRITE(3100,*) gammapast33_1
WRITE(3100,*) d2
WRITE(3100,*) d18
WRITE(3100,*) d19
WRITE(3100,*) d20
WRITE(3100,*) d21
WRITE(3100,*) d22
WRITE(3100,*) d23
WRITE(3100,*) d24
WRITE(3100,*) d25
WRITE(3100,*) d26
WRITE(3100,*) d27
WRITE(3100,*) d28
WRITE(3100,*) d52
WRITE(3100,*) d29
WRITE(3100,*) d30
WRITE(3100,*) d31
WRITE(3100,*) delta
WRITE(3100,*) d
WRITE(3100,*) d32
WRITE(3100,*) plus
WRITE(3100,*) d5j2
WRITE(3100,*) d5j3
WRITE(3100,*) d53
WRITE(3100,*) d533
WRITE(3100,*) d523
WRITE(3100,*) wd
WRITE(3100,*) wd3
WRITE(3100,*) wd2
WRITE(3100,*) tencoef
WRITE(3100,*) tencoef1
WRITE(3100,*) tencoef2
WRITE(3100,*) wd52
WRITE(3100,*) d5t2
WRITE(3100,*) d5t3
WRITE(3100,*) d5t4
WRITE(3100,*) wpen
WRITE(3100,*) wvar
WRITE(3100,*) v74
WRITE(3100,*) v75
WRITE(3100,*) v76
WRITE(3100,*) v77
WRITE(3100,*) v78
WRITE(3100,*) v79
WRITE(3100,*) wpen1
WRITE(3100,*) por
WRITE(3100,*) prop
WRITE(3100,*) tencoef3
WRITE(3100,*) tencoef31
WRITE(3100,*) d5j22
WRITE(3100,*) d5j23
WRITE(3100,*) d5j24
WRITE(3100,*) w2_74
WRITE(3100,*) w2_75
WRITE(3100,*) w2_76
WRITE(3100,*) w2_77
WRITE(3100,*) w2_78
WRITE(3100,*) w2_79
WRITE(3100,*) w3_74
WRITE(3100,*) w3_75
WRITE(3100,*) w3_76
WRITE(3100,*) w3_77
WRITE(3100,*) w3_78
WRITE(3100,*) w3_79
WRITE(3100,*) s5_1
WRITE(3100,*) s5_2
WRITE(3100,*) s1_55
WRITE(3100,*) d55
WRITE(3100,*) s5_3
WRITE(3100,*) s56_1
WRITE(3100,*) s56_2
WRITE(3100,*) s56_3
WRITE(3100,*) s3_1
WRITE(3100,*) s3_2
WRITE(3100,*) s3_3
WRITE(3100,*) tencoef22
WRITE(3100,*) ""



!---------------------------
!ii. Checking param values 
!---------------------------

!PRINT*, ''
PRINT*, 'alpha_1=', alpha_1
PRINT*, 'alpha_2=', alpha_2
PRINT*, 'alpha_3=', alpha_3
PRINT*, 'beta_1=', beta_1
PRINT*, 'beta_2=', beta_2
PRINT*, 'beta_3=', beta_3
PRINT*, 'gammapast11_0=', gammapast11_0
PRINT*, 'gammapast11_1=', gammapast11_1
PRINT*, 'gammapast33_0=', gammapast33_0
PRINT*, 'gammapast33_1=', gammapast33_1
PRINT*, 'd1_1=', d1_1
PRINT*, 'd1_2=', d1_2
PRINT*, 'd1_3=', d1_3
PRINT*, 'xi_1=', xi_1
PRINT*, 'xi_21=', xi_21
PRINT*, 'xi_11 =', xi_11
PRINT*, 'xi_14 =', xi_14
PRINT*, 'xi_15 =', xi_15
PRINT*, 'xi_2=', xi_2
PRINT*, 'xi_24=', xi_24
PRINT*, 'xi_25 =', xi_25
PRINT*, 'xi_26 =', xi_26
PRINT*, 'xi_3=', xi_3
PRINT*, 'xi_32 =', xi_32
PRINT*, 'xi_33 =', xi_33
PRINT*, 'xi_34 =', xi_34
PRINT*, 'cpen=', cpen
PRINT*, 'p_1=', p_1
!PRINT*, 'phk2=', phk2
PRINT*, 'p_2 =', p_2
PRINT*, 'p_3 =', p_3
PRINT*, 'gammap_01=', gammap_01
!PRINT*, 'gammap_11=', gammap_11
PRINT*, 'gammap_02=', gammap_02
PRINT*, 'gammap_03=', gammap_03
PRINT*, 'gammap_04=', gammap_04
!PRINT*, 's_0=', s_0
PRINT*, 's_1=', s_1
PRINT*, 's_2=', s_2
PRINT*, 's_3=', s_3
!PRINT*, 'sigma_eps=', sigma_eps
PRINT*, 's1_5=', s1_5
PRINT*, 's_002=', s_002
PRINT*, 's_003=', s_003
PRINT*, 'sigma1_u=', sigma1_u
PRINT*, 'sigma2_u=', sigma2_u
PRINT*, 'sigma3_u=', sigma3_u
PRINT*, 'sigma32_u=', sigma32_u
PRINT*, 'sigma33_u=', sigma33_u
PRINT*, 'sigma34_u=', sigma34_u
!PRINT*, 'w_70=', w_70
!PRINT*, 'w_71=', w_71
!PRINT*, 'w_72=', w_72
!PRINT*, 'w_73=', w_73
PRINT*, 'w_74=', w_74
PRINT*, 'w_75=', w_75
PRINT*, 'w_76=', w_76
PRINT*, 'w_77=', w_77
PRINT*, 'w_78=', w_78
PRINT*, 'w_79=', w_79
PRINT*, 's1_52=', s1_52
PRINT*, 's1_53=', s1_53
PRINT*, 's1_54=', s1_54
PRINT*, 'd2=', d2
!PRINT*, 'd4=', d4
PRINT*, 'd6=', d6
PRINT*, 'd9=', d9
PRINT*, 'd10=', d10
!PRINT*, 'd11=', d11
!PRINT*, 'phk3=', phk3
!PRINT*, 'd7=', d7
!PRINT*, 'd8=', d8
PRINT*, 'd5=', d5
!PRINT*, 'd1=', d1
!PRINT*, 's_4 =', s_4
PRINT*, 'd12 =', d12
!PRINT*, 'd13=', d13
!PRINT*, 'd14=', d14
!PRINT*, 'd15=', d15
PRINT*, 'd=', d
PRINT*, 'd16=', d16
!PRINT*, 'd17=', d17
PRINT*, 'sigma22_u=', sigma22_u
PRINT*, 'sigma23_u=', sigma23_u
PRINT*, 'sigma24_u=', sigma24_u
PRINT*, 's_0022 =', s_0022
PRINT*, 's_0023 =', s_0023
PRINT*, 's_0024 =', s_0024
PRINT*, 's_0032 =', s_0032
PRINT*, 's_0033 =', s_0033
PRINT*, 's_0034 =', s_0034
PRINT*, 'sigma12_u=', sigma12_u
PRINT*, 'sigma13_u=', sigma13_u
PRINT*, 'sigma14_u=', sigma14_u
!PRINT*, 'd2=', d2
PRINT*, 'd18=', d18
PRINT*, 'd19=', d19
PRINT*, 'd20=', d20
PRINT*, 'd21=', d21
PRINT*, 'd22=', d22
PRINT*, 'd23=', d23
PRINT*, 'd24=', d24
PRINT*, 'd25=', d25
PRINT*, 'd26=', d26
PRINT*, 'd27=', d27
PRINT*, 'd28=', d28
PRINT*, 'd52=', d52
PRINT*, 'd29=', d29
PRINT*, 'd30=', d30
PRINT*, 'd31=', d31
PRINT*, 'd32=', d32
PRINT*, 'plus=', plus
PRINT*, 'd5j2=', d5j2
PRINT*, 'd5j3=', d5j3
PRINT*, 'd53=', d53
PRINT*, 'd533=', d533
PRINT*, 'd523=', d523
PRINT*, 'wd=', wd
PRINT*, 'wd3=', wd3
PRINT*, 'wd2=', wd2
PRINT*, 'tencoef=', tencoef
PRINT*, 'tencoef1=', tencoef1
PRINT*, 'tencoef2=', tencoef2
PRINT*, 'wd52 =', wd52
PRINT*, 'd5t2 =', d5t2
PRINT*, 'd5t3 =', d5t3
PRINT*, 'd5t4 =', d5t4
PRINT*, 'wpen =', wpen
PRINT*, 'wvar=', wvar
PRINT*, 'v74=', v74
PRINT*, 'v75=', v75  
PRINT*, 'v76=', v76
PRINT*, 'v77=', v77
PRINT*, 'v78=', v78
PRINT*, 'v79=', v79
PRINT*, 'wpen1 =', wpen1
PRINT*, 'por =', por
PRINT*, 'prop =', prop
PRINT*, 'tencoef3=', tencoef3
PRINT*, 'tencoef31 =', tencoef31
PRINT*, 'd5j22 =', d5j22
PRINT*, 'd5j23 =', d5j23
PRINT*, 'd5j24 =', d5j24
PRINT*, 'w2_74=', w2_74
PRINT*, 'w2_75=', w2_75
PRINT*, 'w2_76=', w2_76
PRINT*, 'w2_77=', w2_77
PRINT*, 'w2_78=', w2_78
PRINT*, 'w2_79=', w2_79
PRINT*, 'w3_74=', w3_74
PRINT*, 'w3_75=', w3_75
PRINT*, 'w3_76=', w3_76
PRINT*, 'w3_77=', w3_77
PRINT*, 'w3_78=', w3_78
PRINT*, 'w3_79=', w3_79
PRINT*, 's5_1  =', s5_1
PRINT*, 's5_2  =', s5_2
PRINT*, 's1_55 =', s1_55
PRINT*, 'd55   =', d55
PRINT*, 's5_3   =', s5_3
PRINT*, 's56_1   =', s56_1
PRINT*, 's56_2   =', s56_2
PRINT*, 's56_3   =', s56_3
PRINT*, 's3_1   =', s3_1
PRINT*, 's3_2   =', s3_2
PRINT*, 's3_3   =', s3_3
PRINT*, 'tencoef22=', tencoef22
PRINT*, ''


!**********************
!(2) Read in the data 
!**********************
ALLOCATE(level_o(totsample,max_t), sal_o(totsample,max_t), perf_o(totsample,max_t))
ALLOCATE(edu(totsample), age(totsample), year(totsample))
ALLOCATE(totnum(totsample))


OPEN(unit=444, file = 'trimmedsample.txt', position='rewind')

i1 = 0
DO i1 = 1, totsample

 READ(444,'(37I6)') totnum(i1), level_o(i1,1), perf_o(i1,1),  & 
           level_o(i1,2), perf_o(i1,2), level_o(i1,3), perf_o(i1,3), &
		   level_o(i1,4), perf_o(i1,4), level_o(i1,5), perf_o(i1,5), &
		   level_o(i1,6), perf_o(i1,6), level_o(i1,7), perf_o(i1,7), &
		   level_o(i1,8), perf_o(i1,8), level_o(i1,9), perf_o(i1,9), &
		   level_o(i1,10), perf_o(i1,10), level_o(i1,11), perf_o(i1,11), edu(i1), age(i1), year(i1), &
		   sal_o(i1,1), sal_o(i1,2), sal_o(i1,3), sal_o(i1,4), sal_o(i1,5), sal_o(i1,6), &
		   sal_o(i1,7), sal_o(i1,8), sal_o(i1,9), sal_o(i1,10), sal_o(i1,11)
 
 IF(totnum(i1)>1) THEN
  PRINT*, 'ind', i1
  PRINT*, 'replicas', totnum(i1)
  PAUSE
 END IF 

 END DO 
CLOSE(unit=444)



!*******************************
!(3) Calculation of the prior
!*******************************
ALLOCATE(prior(totsample,typesno))
ALLOCATE(year0(totsample), year1(totsample), year2(totsample), year3(totsample),year4(totsample),&
         year5(totsample), year6(totsample), year7(totsample), year8(totsample),year9(totsample))


year0=0
year1=0
year2=0
year3=0
year4=0
year5=0
year6=0
year7=0
year8=0
year9=0


 
i1 = 0
i2 = 0
DO i1 = 1, totsample

 IF(year(i1)==70)THEN
  year0(i1)=1

 ELSE IF(year(i1)==71)THEN
  year1(i1)=1

 ELSE IF(year(i1)==72)THEN
  year2(i1)=1

 ELSE IF(year(i1)==73)THEN
  year3(i1)=1

 ELSE IF(year(i1)==74)THEN
  year4(i1)=1

 ELSE IF(year(i1)==75)THEN
  year5(i1)=1

 ELSE IF(year(i1)==76)THEN
  year6(i1)=1

 ELSE IF(year(i1)==77)THEN
  year7(i1)=1

 ELSE IF(year(i1)==78)THEN
  year8(i1)=1

 ELSE IF(year(i1)==79)THEN
  year9(i1)=1

 END IF
 


 IF(gammap_01+gammap_11*REAL(edu(i1),dp) >= DLOG(1.797693134862300E+308_dp)) THEN

  prior(i1,1) = 1.0_dp-tiny35

 ELSE 


 prior(i1,1) = (  DEXP(gammap_01+gammap_11*REAL(edu(i1),dp)) )/(  1.0_dp+DEXP(gammap_01+gammap_11*REAL(edu(i1),dp))  )


END IF

!PRINT*, 'prior 1', prior(i1,1), 'for ind', i1

 IF(gammap_02+gammap_12*REAL(edu(i1),dp) >= DLOG(1.797693134862300E+308_dp)) THEN

  prior(i1,2) = 1.0_dp-tiny35
!  WRITE (3333,'(I6,F16.10)') edu(i1), prior(i1,2)

 ELSE 


 prior(i1,2) = (   DEXP(gammap_02+gammap_12*REAL(edu(i1),dp))   )/(   1.0_dp+DEXP(gammap_02+gammap_12*REAL(edu(i1),dp))   )

! WRITE (3333,'(I6,F16.10)') edu(i1), prior(i1,2)

END IF


!PRINT*, 'prior 2', prior(i1,2), 'for ind', i1

IF(gammap_03+gammap_13*REAL(edu(i1),dp) >= DLOG(1.797693134862300E+308_dp)) THEN

  prior(i1,3) = 1.0_dp-tiny35


 ELSE 


 prior(i1,3) = (   DEXP(gammap_03+gammap_13*REAL(edu(i1),dp))    )/(   1.0_dp+DEXP(gammap_03+gammap_13*REAL(edu(i1),dp))   )


END IF

!PRINT*, 'prior 3', prior(i1,3), 'for ind', i1


IF(gammap_04 +gammap_14*REAL(edu(i1),dp)  >=  DLOG(1.797693134862300E+308_dp)) THEN

  prior(i1,4) = 1.0_dp-tiny35
  


ELSE 


 prior(i1,4) = (  DEXP(gammap_04 +gammap_14*REAL(edu(i1),dp))   )/(  1.0_dp+DEXP(gammap_04 +gammap_14*REAL(edu(i1),dp))   )

END IF


!PRINT*, 'prior 4', prior(i1,4), 'for ind', i1
!WRITE(3000,*) 'prior 4', prior(i1,4)

 IF (i1==1 .and. i2==1) THEN
  WRITE(3000,*) "Ind 1: Edu", REAL(edu(i1),dp)
  WRITE(3000,*) "Ind 1: Edu^2", REAL((edu(i1))**2,dp)
  WRITE(3000,*) "Ind 1: Age",   REAL(age(i1),dp)
  WRITE(3000,*) "Ind 1: Age^2", REAL((age(i1))**2,dp)
  WRITE(3000,*) "Ind 1: Edu*Age", REAL(age(i1)*edu(i1),dp)
 END IF


END DO




!*********************************
!(4) Value function computation
!*********************************
ALLOCATE(tenure_2_L1(totsample),tenure_2_L2(totsample))
ALLOCATE(tenure_3_L1(totsample),tenure_3_L2(totsample),tenure_3_L3(totsample))
ALLOCATE(tenure_4_L1(totsample),tenure_4_L2(totsample),tenure_4_L3(totsample))
ALLOCATE(tenure_5_L1(totsample),tenure_5_L2(totsample),tenure_5_L3(totsample))
ALLOCATE(tenure_6_L1(totsample),tenure_6_L2(totsample),tenure_6_L3(totsample))
ALLOCATE(tenure_7_L1(totsample),tenure_7_L2(totsample),tenure_7_L3(totsample))



tenure_2_L1 = 0
tenure_2_L2 = 0

tenure_3_L1 = 0
tenure_3_L2 = 0
tenure_3_L3 = 0

tenure_4_L1 = 0
tenure_4_L2 = 0
tenure_4_L3 = 0


tenure_5_L1 = 0
tenure_5_L2 = 0
tenure_5_L3 = 0


tenure_6_L1 = 0
tenure_6_L2 = 0
tenure_6_L3 = 0



tenure_7_L1 = 0
tenure_7_L2 = 0
tenure_7_L3 = 0

i1 = 0

DO i1 = 1, totsample
 !---------------
 ! Second period
 !---------------
 IF (level_o(i1,2)==1) THEN 

  tenure_2_L1(i1) = 1
  tenure_2_L2(i1) = 0
 
 ELSE IF (level_o(i1,2)==2) THEN 
  
  tenure_2_L1(i1) = 0
  tenure_2_L2(i1) = 1
 
 END IF

 !--------------
 ! Third period
 !--------------
 IF (level_o(i1,3)==1) THEN 

  tenure_3_L1(i1) = 1
  tenure_3_L2(i1) = 0
  tenure_3_L3(i1) = 0
 
 ELSE IF (level_o(i1,3)==2) THEN 
  
  tenure_3_L1(i1) = 0
  tenure_3_L2(i1) = 1
  tenure_3_L3(i1) = 0


 ELSE IF (level_o(i1,3)>=3) THEN 
  
  tenure_3_L1(i1) = 0
  tenure_3_L2(i1) = 0
  tenure_3_L3(i1) = 1
 
 END IF


 !---------------
 ! Fourth period
 !---------------
 IF (level_o(i1,4)==1) THEN 

  tenure_4_L1(i1) = 1
  tenure_4_L2(i1) = 0
  tenure_4_L3(i1) = 0
 
 ELSE IF (level_o(i1,4)==2) THEN 
  
  tenure_4_L1(i1) = 0
  tenure_4_L2(i1) = 1
  tenure_4_L3(i1) = 0


 ELSE IF (level_o(i1,4)>=3) THEN 
  
  tenure_4_L1(i1) = 0
  tenure_4_L2(i1) = 0
  tenure_4_L3(i1) = 1
 
 END IF


 !---------------
 ! Fifth period
 !---------------
 IF (level_o(i1,5)==1) THEN 

  tenure_5_L1(i1) = 1
  tenure_5_L2(i1) = 0
  tenure_5_L3(i1) = 0
 
 ELSE IF (level_o(i1,5)==2) THEN 
  
  tenure_5_L1(i1) = 0
  tenure_5_L2(i1) = 1
  tenure_5_L3(i1) = 0


 ELSE IF (level_o(i1,5)>=3) THEN 
  
  tenure_5_L1(i1) = 0
  tenure_5_L2(i1) = 0
  tenure_5_L3(i1) = 1
 
 END IF


 !---------------
 ! Sixth period
 !---------------
 IF (level_o(i1,6)==1) THEN 

  tenure_6_L1(i1) = 1
  tenure_6_L2(i1) = 0
  tenure_6_L3(i1) = 0
 
 ELSE IF (level_o(i1,6)==2) THEN 
  
  tenure_6_L1(i1) = 0
  tenure_6_L2(i1) = 1
  tenure_6_L3(i1) = 0


 ELSE IF (level_o(i1,6)>=3) THEN 
  
  tenure_6_L1(i1) = 0
  tenure_6_L2(i1) = 0
  tenure_6_L3(i1) = 1
 
 END IF


 !----------------
 ! Seventh period
 !----------------
 IF (level_o(i1,7)==1) THEN 

  tenure_7_L1(i1) = 1
  tenure_7_L2(i1) = 0
  tenure_7_L3(i1) = 0
 
 ELSE IF (level_o(i1,7)==2) THEN 
  
  tenure_7_L1(i1) = 0
  tenure_7_L2(i1) = 1
  tenure_7_L3(i1) = 0


 ELSE IF (level_o(i1,7)>=3) THEN 
  
  tenure_7_L1(i1) = 0
  tenure_7_L2(i1) = 0
  tenure_7_L3(i1) = 1
 
 END IF




END DO

CALL value_infinite(value8,task,V_0,V_11, V_21, V_31, V_12, V_22, V_32, V_13, V_23, V_33, &
     V_14, V_24, V_34, V_15, V_25, V_35, V_16, V_26, V_36, V_17, V_27, V_37, V_18, V_28, V_38, phi_1_grid_point,phi_1_grid_point_max, phi_1_star,phi_2_grid_point,&
	 phi_2_star,phi_3_star)


lower_grid_point = phi_1_grid_point
upper_grid_point = phi_1_grid_point_max
value1 = 0.0_dp
value2 = 0.0_dp
value3 = 0.0_dp
value4 = 0.0_dp
value5 = 0.0_dp
value6 = 0.0_dp
value7 = 0.0_dp



!********************************************************
!(5) Allocate the size of the prior for what follows
!********************************************************
prior_sim_dimension = typesno



!***************************************************************************************
!(6) Loops to compute emmployment and assignment probabilities to levels (L0,L1,L2,L3):
!    prior(i1), individual(i2), theoretical type(i3), tenure(i4)
!***************************************************************************************
PRINT*, "1. Begin the loop on inds and priors"

ALLOCATE(like_prior_type(totsample,prior_sim_dimension,2))

!====================
i1 = 0
DO i1 = 1, totsample
!====================

!IF(i1==1385) THEN
!PRINT*, 'ind', i1
! PRINT*, 'age', age(i1)
! PRINT*, 'edu', edu(i1)
! PRINT*, 'year', year(i1)
! PRINT*, 'level_o(i1,1)=', level_o(i1,1)
! PRINT*, 'level_o(i1,2)=', level_o(i1,2)
! PRINT*, 'level_o(i1,3)=', level_o(i1,3)
! PRINT*, 'level_o(i1,4)=', level_o(i1,4)
! PRINT*, 'level_o(i1,5)=', level_o(i1,5)
! PRINT*, 'level_o(i1,6)=', level_o(i1,6)
! PRINT*, 'level_o(i1,7)=', level_o(i1,7)
! PRINT*, 'perf_o(i1,1)=',  perf_o(i1,1)
! PRINT*, 'perf_o(i1,2)=',  perf_o(i1,2)
! PRINT*, 'perf_o(i1,3)=',  perf_o(i1,3)
! PRINT*, 'perf_o(i1,4)=',  perf_o(i1,4)
! PRINT*, 'perf_o(i1,5)=',  perf_o(i1,5)
! PRINT*, 'perf_o(i1,6)=',  perf_o(i1,6)
! PRINT*, 'perf_o(i1,7)=',  perf_o(i1,7)
! PRINT*, 'log sal_o(i1,1)=',   DLOG(REAL(sal_o(i1,1),dp))
! PRINT*, 'log sal_o(i1,2)=',   DLOG(REAL(sal_o(i1,2),dp))
! PRINT*, 'log sal_o(i1,3)=',   DLOG(REAL(sal_o(i1,3),dp))
! PRINT*, 'log sal_o(i1,4)=',   DLOG(REAL(sal_o(i1,4),dp))
! PRINT*, 'log sal_o(i1,5)=',   DLOG(REAL(sal_o(i1,5),dp))
! PRINT*, 'log sal_o(i1,6)=',   DLOG(REAL(sal_o(i1,6),dp))
! PRINT*, 'log sal_o(i1,7)=',   DLOG(REAL(sal_o(i1,7),dp))
!END IF


!----------------------------------
!i. Indicator function for tenure
!----------------------------------
i66=0
ind_perf_miss = 0
ind_exit = 0



DO i66 = 1, tot_tenure

 !------------------------------------------------------------------------------
 !ii. End of individual's history (both level and performance are first missing)
 !------------------------------------------------------------------------------
 ! NOTE: the loop for itenure is on PAST years of empployment: itenure = i66-1,
 ! implies that level(i66), perf(i66-1) and salary(i66-1) are computed
 IF (level_o(i1,i66)==-2 .and. perf_o(i1,i66)==-2)THEN

  ind_exit(i66) = 1
  EXIT
                   
END IF

END DO


!----------------------------------------------------------------------------------------
!iii. Assign tenure: there is a correction for the fact that, when the perf is missing 
!     in the 1st period, it would look like the person has no tenure at the firm
!----------------------------------------------------------------------------------------
IF (SUM(ind_exit)>0)THEN
 IF(i66 >=2) THEN  
  
 itenure = i66
   
 ELSE IF (i66 ==1) THEN
  itenure = i66
  PAUSE
 END IF

ELSE
 !itenure is the loop over the performance ratings: once can compute the probability of the level until the following period
 itenure = tot_tenure
END IF



!------------------------------------------------
!iv. Check tenure assignment for the first 5 inds
!------------------------------------------------
IF(i1>=1 .and. i1<=5)THEN
 WRITE(3000,*) "Tenure for ind", i1, "is", itenure
! WRITE(3000,*) "ind_perf_miss", i1, "is", ind_perf_miss
! WRITE(3000,*) "ind_exit", i1, "is", ind_exit 
END IF 
 


!-------------------------
!v. Loop over prior types
!-------------------------

!==============================
i2 = 0
DO i2 = 1, typesno
!==============================



!---------------------------------------------------------------
!vi. Set the INITIAL INDIVIDUAL HISTORY by IND, PRIOR and TYPE
!   1. like_prior_type(CAP_N,prior_sim_dimension,2)
!---------------------------------------------------------------
i3 = 0
DO i3 = 1, 2  
 like_prior_type(i1,i2,i3) = 0.0_dp
END DO
 


!------------------------------------------ 
!vii. Set the initial probs for the LEVELS 
!------------------------------------------
!prob_level    = 0.0_dp

prob_level_1  = 1.0_dp
prob_level_2  = 1.0_dp
prob_level_3  = 1.0_dp
prob_level_4  = 1.0_dp
prob_level_5  = 1.0_dp
prob_level_6  = 1.0_dp
prob_level_7  = 1.0_dp
prob_level_8  = 1.0_dp
prob_level_9  = 1.0_dp
prob_level_10 = 1.0_dp



prob_wage_1  = 1.0_dp
prob_wage_2  = 1.0_dp
prob_wage_3  = 1.0_dp
prob_wage_4  = 1.0_dp
prob_wage_5  = 1.0_dp
prob_wage_6  = 1.0_dp
prob_wage_7  = 1.0_dp
prob_wage_8  = 1.0_dp
prob_wage_9  = 1.0_dp
prob_wage_10 = 1.0_dp

!----------------------------------------------------------------
!viii. Set the initial probs for the RATINGS (dimension-2 arrays)
!----------------------------------------------------------------  
!prob_perf    = 0.0_dp

prob_perf_1  = 1.0_dp
prob_perf_2  = 1.0_dp
prob_perf_3  = 1.0_dp
prob_perf_4  = 1.0_dp
prob_perf_5  = 1.0_dp
prob_perf_6  = 1.0_dp
prob_perf_7  = 1.0_dp
prob_perf_8  = 1.0_dp
prob_perf_9  = 1.0_dp
prob_perf_10 = 1.0_dp


!------------------------------------------------------------------------------------------
!ix. Loop over all possible TRUE ratings using Bayes's rule for the posterior 
!    phi_bayes(phi,tenure,task1,outcome1,task2,outcome2,task3,outcome3,task4,outcome4,
!           task5,outcome5,task6,outcome6,task7,outcome7,task8,outcome8,task9,outcome9)
!    recall that value_op(i1,3) = value_cont(value,prior(i1,3))
!-------------------------------------------------------------------------------------------
!====================
IF(itenure>=1) THEN
!====================
 
 tten = 1
 bupdate = 0.0_dp
 bupdate = prior(i1,i2)
 
   
 !V_0_lik(1) = 0.0_dp
 CALL sub_interp1(phi_grid,V_0,bupdate,phi_grid_size,z)  
 V_0_lik(1)   = z
 CALL sub_interp1(phi_grid,V_11,bupdate,phi_grid_size,z)        
 V_1_lik(1)   = z
 CALL sub_interp1(phi_grid,V_21,bupdate,phi_grid_size,z)
 V_2_lik(1)   = z
 CALL sub_interp1(phi_grid,V_31,bupdate,phi_grid_size,z)
 V_3_lik(1)   = z
 V_lik(1)     = DMAX1(V_0_lik(1),V_1_lik(1),V_2_lik(1),V_3_lik(1))
 !V_lik(1)     = value_cont(value,bupdate)  
 prob_level_1 = problevel(level_o(i1,1),V_lik(1),V_0_lik(1),V_1_lik(1),V_2_lik(1),V_3_lik(1),tten,-4)



 IF (sal_o(i1,1) .ne. -2) THEN
  prob_wage_1 = prob_wage(i2,level_o(i1,1),level_o(i1,1),prior(i1,i2),sal_o(i1,1),V_1_lik(1),V_2_lik(1),V_3_lik(1), &
			       value_cont(value1,prior(i1,i2)), V_lik(1), &
			       edu(i1),p1,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), &
				   year4(i1), year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp))
 
  ELSE 

  prob_wage_1 = 1.0_dp
 
 END IF



!===============
DO p1_bis = 0,1 
!===============
   
    !IF (level_o(i1,2) > level_o(i1,1)) THEN
    !  p1 = 1
    !ELSE
	  p1 = p1_bis
    !END IF
 	

 !______________________________________________________
 !t1: begin per-period probability of level and rating
 !______________________________________________________

 !--------------------------------------------------------------------
 !1. Update after true performance, initialized at a particular prior
 !--------------------------------------------------------------------
  tten = 2
  bupdate2 = 0.0_dp
  bupdate2 = phi_bayes(prior(i1,i2),1, level_o(i1,1), p1, level_o(i1,2), p2, &
           level_o(i1,3), p3, level_o(i1,4), p4, level_o(i1,5), p5, level_o(i1,6), p6,&
		   level_o(i1,7), p7, level_o(i1,8), p8, level_o(i1,9), p9, i2, tten-1)
 

 !-------------------------------------------
 !2. Input into the prob of observed level
 !-------------------------------------------
   !V_0_lik(2)   = 0.0_dp
   CALL sub_interp1(phi_grid,V_0,bupdate2,phi_grid_size,z)  
   V_0_lik(2)   = z
   CALL sub_interp1(phi_grid,V_12,bupdate2,phi_grid_size,z)        
   V_1_lik(2)   = z
   CALL sub_interp1(phi_grid,V_22,bupdate2,phi_grid_size,z)
   V_2_lik(2)   = z
   CALL sub_interp1(phi_grid,V_32,bupdate2,phi_grid_size,z)
   V_3_lik(2)   = z
   V_lik(2)     = DMAX1(V_0_lik(2),V_1_lik(2),V_2_lik(2),V_3_lik(2))
   !V_lik(2)    = value_cont(value,bupdate)       
   


 !------------------------------------------------------------------
 !3. Accumulating probability of the SEQUENCE of observed levels
 !a.  (over all possible values of the TRUE RATINGS)
 !b.  problevel(level,value,value1,value2,value3)
 !-------------------------------------------------------------------
  
  prob_level_2 = problevel(level_o(i1,2),V_lik(2),V_0_lik(2),V_1_lik(2),V_2_lik(2),V_3_lik(2),tten,level_o(i1,1))



 IF(sal_o(i1,2) .ne. -2 .and. level_o(i1,2) .ne. -2) THEN
  prob_wage_2  = prob_wage(i2,level_o(i1,2),level_o(i1,1),bupdate2,sal_o(i1,2),V_1_lik(2),V_2_lik(2),V_3_lik(2),&
				      value_cont(value2,bupdate2), V_lik(2), &
				      edu(i1),p1,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1) ,REAL(tten-1,dp))



  ELSE IF(sal_o(i1,2) == -2) THEN
 
   
   prob_wage_2  = 1.0_dp
  
 
 END IF



 !-----------------------------------------------------------------------------------------------------------------------------
 !4. Accumulating prob of the SEQUENCE of observed ratings
 !  1. prob_joint_rating =  prob_joint_rating(hk,task,task1,task2,prevrat1,prevrat2,outcome_o,outcome,phi,type_prior,ftenure)
 !  2. p_success = p_success(hk,task,outcome, priortype)
 !-----------------------------------------------------------------------------------------------------------------------------
 i3 = 0 !loop over types
 IF (perf_o(i1,1) .ne. -2)THEN
  DO i3 = 1,2 
	
	prob_perf_1(i3) = prob_joint_rating(i3,level_o(i1,1),0,level_o(i1,2),0,0,perf_o(i1,1),p1,bupdate,i2,tten-1)
	

  IF (i1==1 .and. i2 ==1) THEN
   WRITE(3000,*) "For ind 1 (prior1), prob_perf_1: ", prob_perf_1(i3), "(type)", i3
   WRITE(3000,*)
  END IF

  END DO
 ELSE

  !p_success(hk,task,outcome, priortype)
  DO i3 = 1,2 
    prob_perf_1(i3) = p_success(i3,level_o(i1,1),p1,i2)
	!prob_perf_1(i3) = 1.0_dp

  END DO
 END IF


!______________________________________________________
! end per-period probability of level and rating, t=1
!______________________________________________________




IF(itenure>=2) THEN
!______________________________________________________
!t=2: begin per-period probability of level and rating
!______________________________________________________
  !================
   DO p2_bis = 0,1
  !================  

    !IF (level_o(i1,3) > level_o(i1,2)) THEN
	!  p2 = 1
    !ELSE
	  p2 = p2_bis
    !END IF
    

 !--------------------------------------------------------------------
 !1. Update after true performance, initialized at a particular prior
 !--------------------------------------------------------------------
  tten = 3
  bupdate3 = 0.0_dp
  bupdate3 = phi_bayes(prior(i1,i2),2, level_o(i1,1), p1, level_o(i1,2), p2, &
           level_o(i1,3), p3, level_o(i1,4), p4, level_o(i1,5), p5, level_o(i1,6), p6,&
		   level_o(i1,7), p7, level_o(i1,8), p8, level_o(i1,9), p9, i2, tten-1)
 

 !-------------------------------------------
 !2. Input into the prob of observed level
 !-------------------------------------------
 
   !V_0_lik(3)   = 0.0_dp
   CALL sub_interp1(phi_grid,V_0,bupdate3,phi_grid_size,z)  
   V_0_lik(3)   = z
   CALL sub_interp1(phi_grid,V_13,bupdate3,phi_grid_size,z)        
   V_1_lik(3)   = z
   CALL sub_interp1(phi_grid,V_23,bupdate3,phi_grid_size,z)
   V_2_lik(3)   = z
   CALL sub_interp1(phi_grid,V_33,bupdate3,phi_grid_size,z)
   V_3_lik(3)   = z
   
   !V_lik(4)     = DMAX1(V_0_lik(4),V_1_lik(4),V_2_lik(4),V_3_lik(4))

   V_lik(3)     = DMAX1(V_0_lik(3),V_1_lik(3),V_2_lik(3),V_3_lik(3))

 !------------------------------------------------------------------
 !3. Accumulating probability of the SEQUENCE of observed levels
 !------------------------------------------------------------------

  IF(level_o(i1,2)==-2) THEN

   prob_level_3 = 1.0_dp
 
   ELSE

   prob_level_3 = problevel(level_o(i1,3),V_lik(3),V_0_lik(3),V_1_lik(3),V_2_lik(3),V_3_lik(3),tten,level_o(i1,2))

   END IF


IF(sal_o(i1,3) .ne. -2 .and. level_o(i1,3) .ne. -2) THEN
  


  prob_wage_3  = prob_wage(i2,level_o(i1,3),level_o(i1,2),bupdate3,sal_o(i1,3),V_1_lik(3),V_2_lik(3),V_3_lik(3),&
				      value_cont(value3,bupdate3), V_lik(3), &
				      edu(i1),p2,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp))

 


  ELSE IF(sal_o(i1,3) == -2) THEN
   
   prob_wage_3  = 1.0_dp
  


 END IF



!-----------------------------------------------------------
!4. Accumulating prob of the SEQUENCE of observed ratings
!-----------------------------------------------------------



  i3 = 0
  !IF (perf_o(i1,1) .ne. -2 .and. perf_o(i1,2) .ne. -2)THEN
  IF (perf_o(i1,2) .ne. -2)THEN
  DO i3 = 1, 2 
   prob_perf_2(i3) = prob_joint_rating(i3,level_o(i1,2),level_o(i1,1),level_o(i1,3),perf_o(i1,1),0,perf_o(i1,2),p2,bupdate2,i2,tten-1)
  END DO
 ELSE
   DO i3 = 1,2 
   !p_success(hk,task,outcome, priortype)
	prob_perf_2(i3) = p_success(i3,level_o(i1,2),p2,i2)

  END DO

 END IF


!______________________________________________________
! end per-period probability of level and rating, t=2
!______________________________________________________



IF(itenure>=3) THEN
!______________________________________________________
!t=3: begin per-period probability of level and rating
!______________________________________________________
  !===============
  DO p3_bis = 0,1
  !===============

    !IF (level_o(i1,4) > level_o(i1,3)) THEN
	!  p3 = 1
    !ELSE
	  p3 = p3_bis
    !END IF


 
 !--------------------------------------------------------------------
 !1. Update after true performance, initialized at a particular prior
 !--------------------------------------------------------------------
  tten = 4 
  bupdate4 = 0.0_dp
  bupdate4 = phi_bayes(prior(i1,i2),3, level_o(i1,1), p1, level_o(i1,2), p2, &
           level_o(i1,3), p3, level_o(i1,4), p4, level_o(i1,5), p5, level_o(i1,6), p6,&
		   level_o(i1,7), p7, level_o(i1,8), p8, level_o(i1,9), p9, i2, tten-1)
 

 !-------------------------------------------
 !2. Input into the prob of observed level
 !-------------------------------------------

 IF(level_o(i1,3) .ne. -2) THEN   

  leveldummy3 = level_o(i1,3)
     
	 IF (level_o(i1,3) >=3) THEN

	  leveldummy3 = 3

	 END IF	 
   
   !V_0_lik(4)   = 0.0_dp
   CALL sub_interp1(phi_grid,V_0,bupdate4,phi_grid_size,z)  
   V_0_lik(4)   = z
   CALL sub_interp1(phi_grid,V_14(:,leveldummy3),bupdate4,phi_grid_size,z)        
   V_1_lik(4)   = z
   CALL sub_interp1(phi_grid,V_24(:,leveldummy3),bupdate4,phi_grid_size,z)
   V_2_lik(4)   = z
   CALL sub_interp1(phi_grid,V_34(:,leveldummy3),bupdate4,phi_grid_size,z)
   V_3_lik(4)   = z

   
   V_lik(4)     = DMAX1(V_0_lik(4),V_1_lik(4),V_2_lik(4),V_3_lik(4))

 
  END IF

 !------------------------------------------------------------------
 !3. Accumulating probability of the SEQUENCE of observed levels
 !-------------------------------------------------------------------
 
 IF(level_o(i1,3)==-2) THEN

 prob_level_4 = 1.0_dp
 
  ELSE

 prob_level_4 = problevel(level_o(i1,4),V_lik(4),V_0_lik(4),V_1_lik(4),V_2_lik(4),V_3_lik(4), tten, level_o(i1,3))

 END IF




IF(sal_o(i1,4) .ne. -2 .and. level_o(i1,4) .ne. -2) THEN
 

  prob_wage_4  =   prob_wage(i2,level_o(i1,4),level_o(i1,3),bupdate4,sal_o(i1,4),V_1_lik(4),V_2_lik(4),V_3_lik(4),&
				      value_cont(value4,bupdate4), V_lik(4), &
				      edu(i1),p3,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp))

 
  IF(level_o(i1,4)==1) THEN

    prob_wage_4  =   prob_wage(i2,level_o(i1,4),level_o(i1,3),bupdate4,sal_o(i1,4),V_1_lik(4),V_2_lik(4),V_3_lik(4),&
				      value_cont(value4,bupdate4), V_lik(4), &
				      edu(i1),p3,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp))

  
  ELSE IF(level_o(i1,4) ==2) THEN


  prob_wage_4  =   prob_wage(i2,level_o(i1,4),level_o(i1,3),bupdate4,sal_o(i1,4),V_1_lik(4),V_2_lik(4),V_3_lik(4),&
				      value_cont(value4,bupdate4), V_lik(4), &
				      edu(i1),p3,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp))



  ELSE IF(level_o(i1,4) ==3) THEN


   prob_wage_4  =   prob_wage(i2,level_o(i1,4),level_o(i1,3),bupdate4,sal_o(i1,4),V_1_lik(4),V_2_lik(4),V_3_lik(4),&
				      value_cont(value4,bupdate4), V_lik(4), &
				      edu(i1),p3,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp))

 


    END IF

 
  IF(level_o(i1,4)>=4) THEN
   prob_wage_4  = 1.0_dp
  END IF

  ELSE IF(sal_o(i1,4) == -2) THEN
   
    
   prob_wage_4  = 1.0_dp
  



 END IF


!-----------------------------------------------------------
!4. Accumulating prob of the SEQUENCE of observed ratings
!-----------------------------------------------------------

 i3 = 0 !loop over types 
 !IF (perf_o(i1,2) .ne. -2 .and. perf_o(i1,3) .ne. -2)THEN
  IF (perf_o(i1,3) .ne. -2) THEN
  DO i3 = 1,2 
   prob_perf_3(i3) = prob_joint_rating(i3,level_o(i1,3),level_o(i1,2),level_o(i1,4),perf_o(i1,2),&
                       perf_o(i1,1),perf_o(i1,3),p3,bupdate3,i2,tten-1)
  END DO
 ELSE
   DO i3 = 1,2 
    !p_success(hk,task,outcome, priortype)
    prob_perf_3(i3) = p_success(i3,level_o(i1,3),p3,i2)
	
  END DO
 END IF

 i3 = 0
 DO i3 = 1,2
  IF(level_o(i1,3)>=3) THEN
   !prob_perf_3(i3) = 1.0_dp
   prob_perf_3(i3) = p_success(i3,level_o(i1,3),p3,i2)
  END IF
 END DO


!______________________________________________________
! end per-period probability of level and rating, t=3
!______________________________________________________


IF(itenure>=4) THEN   
!_______________________________________________________
! t=4: begin per-period probability of level and rating
!_______________________________________________________
!================
 DO p4_bis = 0,1
!================ 
    
	!IF (level_o(i1,5) > level_o(i1,4)) THEN
	!  p4 = 1
    !ELSE
	  p4 = p4_bis
	!END IF


 !--------------------------------------------------------------------
 !1. Update after true performance, initialized at a particular prior
 !--------------------------------------------------------------------
   tten = 5
  bupdate5 = 0.0_dp
  bupdate5 = phi_bayes(prior(i1,i2),4, level_o(i1,1), p1, level_o(i1,2), p2, &
           level_o(i1,3), p3, level_o(i1,4), p4, level_o(i1,5), p5, level_o(i1,6), p6,&
		   level_o(i1,7), p7, level_o(i1,8), p8, level_o(i1,9), p9, i2, tten-1)
 

 !-------------------------------------------
 !2. Input into the prob of observed level
 !-------------------------------------------

 IF(level_o(i1,4) .ne. -2) THEN   

   leveldummy3 = level_o(i1,3) 
   leveldummy4 = level_o(i1,4) 

     
	 IF (level_o(i1,3) >=3) THEN

	  leveldummy3 = 3

	 END IF

	 IF (level_o(i1,4) >=3) THEN

	  leveldummy4 = 3

	 END IF
   
   !V_0_lik(5)   = 0.0_dp
   CALL sub_interp1(phi_grid,V_0,bupdate5,phi_grid_size,z)  
   V_0_lik(5)   = z
   CALL sub_interp1(phi_grid,V_15(:,leveldummy3,leveldummy4),bupdate5,phi_grid_size,z)        
   V_1_lik(5)   = z
   CALL sub_interp1(phi_grid,V_25(:,leveldummy3,leveldummy4),bupdate5,phi_grid_size,z)
   V_2_lik(5)   = z
   CALL sub_interp1(phi_grid,V_35(:,leveldummy3,leveldummy4),bupdate5,phi_grid_size,z)
   V_3_lik(5)   = z
  
   V_lik(5)     = DMAX1(V_0_lik(5),V_1_lik(5),V_2_lik(5),V_3_lik(5))

   

  END IF
  
 !----------------------------------------------------------------
 !3. Accumulating probability of the SEQUENCE of observed levels
 !----------------------------------------------------------------

 IF(level_o(i1,4)==-2) THEN

  prob_level_5 = 1.0_dp
 
  ELSE

 prob_level_5 = problevel(level_o(i1,5),V_lik(5),V_0_lik(5),V_1_lik(5),V_2_lik(5),V_3_lik(5),tten, level_o(i1,4) )


 END IF



 IF(sal_o(i1,5) .ne. -2 .and. level_o(i1,5) .ne. -2) THEN
 
  prob_wage_5  = prob_wage(i2,level_o(i1,5),level_o(i1,4),bupdate5,sal_o(i1,5),V_1_lik(5),V_2_lik(5),V_3_lik(5),&
				      value_cont(value5,bupdate5), V_lik(5), &
				      edu(i1),p4,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp))

  IF(level_o(i1,5)>=4) THEN
   prob_wage_5  = 1.0_dp
  END IF


  IF(level_o(i1,5)==1) THEN
   prob_wage_5  = prob_wage(i2,level_o(i1,5),level_o(i1,4),bupdate5,sal_o(i1,5),V_1_lik(5),V_2_lik(5),V_3_lik(5),&
				      value_cont(value5,bupdate5), V_lik(5), &
				      edu(i1),p4,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp) )

  
  ELSE IF (level_o(i1,5)==2) THEN
    prob_wage_5  = prob_wage(i2,level_o(i1,5),level_o(i1,4),bupdate5,sal_o(i1,5),V_1_lik(5),V_2_lik(5),V_3_lik(5),&
				      value_cont(value5,bupdate5), V_lik(5), &
				      edu(i1),p4,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp) )
  

  ELSE IF (level_o(i1,5)==3) THEN
   prob_wage_5  = prob_wage(i2,level_o(i1,5),level_o(i1,4),bupdate5,sal_o(i1,5),V_1_lik(5),V_2_lik(5),V_3_lik(5),&
				      value_cont(value5,bupdate5), V_lik(5), &
				      edu(i1),p4,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp) )

  
  END IF


  ELSE IF(sal_o(i1,5) == -2) THEN
   
   
   prob_wage_5  = 1.0_dp
  



 END IF



!-----------------------------------------------------------
!4. Accumulating prob of the SEQUENCE of observed ratings
!-----------------------------------------------------------

 i3 = 0
  !IF (perf_o(i1,3) .ne. -2 .and. perf_o(i1,4) .ne. -2)THEN
  IF (perf_o(i1,4) .ne. -2) THEN
  DO i3 = 1, 2 
   prob_perf_4(i3) = prob_joint_rating(i3,level_o(i1,4),level_o(i1,3),level_o(i1,5),perf_o(i1,3),&
                       perf_o(i1,2), perf_o(i1,4),p4,bupdate4,i2,tten-1)
  END DO
 ELSE
   DO i3 = 1,2 
   !p_success(hk,task,outcome, priortype)
    prob_perf_4(i3) = p_success(i3,level_o(i1,4),p4,i2)
    !prob_perf_4(i3) = 1.0_dp
  END DO
 END IF


i3 = 0
 DO i3 = 1,2
  IF(level_o(i1,4)>=3) THEN
   prob_perf_4(i3) = p_success(i3,level_o(i1,4),p4,i2)
   !prob_perf_4(i3) = 1.0_dp
  END IF
 END DO




!_____________________________________________________
! end per-period probability of level and rating, t=4
!_____________________________________________________


IF(itenure>=5) THEN
  !===============
  DO p5_bis = 0,1  
  !===============

	!IF (level_o(i1,6) > level_o(i1,5)) THEN
	!  p5 = 1
    !ELSE
	  p5 = p5_bis
    !END IF


!________________________________________________________
! t=5: begin per-period probability of level and rating
!________________________________________________________

 !--------------------------------------------------------------------
 !1. Update after true performance, initialized at a particular prior
 !--------------------------------------------------------------------
  tten = 6
  bupdate6 = 0.0_dp
  bupdate6 = phi_bayes(prior(i1,i2),5, level_o(i1,1), p1, level_o(i1,2), p2, &
           level_o(i1,3), p3, level_o(i1,4), p4, level_o(i1,5), p5, level_o(i1,6), p6,&
		   level_o(i1,7), p7, level_o(i1,8), p8, level_o(i1,9), p9, i2, tten-1)
 

 !-------------------------------------------
 !2. Input into the prob of observed level
 !-------------------------------------------

  IF(level_o(i1,5) .ne. -2) THEN  
  
   leveldummy3 = level_o(i1,3) 
   leveldummy4 = level_o(i1,4) 
   leveldummy5 = level_o(i1,5) 

     
	 IF (level_o(i1,3) >=3) THEN

	  leveldummy3 = 3

	 END IF

	 IF (level_o(i1,4) >=3) THEN

	  leveldummy4 = 3

	 END IF

	 IF (level_o(i1,5) >=3) THEN

	  leveldummy5 = 3

	 END IF
 
 
   
     
   !V_0_lik(6)   = 0.0_dp
   CALL sub_interp1(phi_grid,V_0,bupdate6,phi_grid_size,z)  
   V_0_lik(6)   = z
   CALL sub_interp1(phi_grid,V_16(:,leveldummy3,leveldummy4,leveldummy5),bupdate6,phi_grid_size,z)        
   V_1_lik(6)   = z
   CALL sub_interp1(phi_grid,V_26(:,leveldummy3,leveldummy4,leveldummy5),bupdate6,phi_grid_size,z)
   V_2_lik(6)   = z
   CALL sub_interp1(phi_grid,V_36(:,leveldummy3,leveldummy4,leveldummy5),bupdate6,phi_grid_size,z)
   V_3_lik(6)   = z

   

   V_lik(6)     = DMAX1(V_0_lik(6),V_1_lik(6),V_2_lik(6),V_3_lik(6))
 
  END IF

 !------------------------------------------------------------------
 !3. Accumulating probability of the SEQUENCE of observed levels
 !-------------------------------------------------------------------
 
  IF(level_o(i1,5)==-2) THEN

   prob_level_6 = 1.0_dp
 
  ELSE

   prob_level_6 = problevel(level_o(i1,6),V_lik(6),V_0_lik(6),V_1_lik(6),V_2_lik(6),V_3_lik(6), tten, level_o(i1,5) )

  END IF


 IF(sal_o(i1,6) .ne. -2 .and. level_o(i1,6) .ne. -2) THEN

  prob_wage_6  = prob_wage(i2,level_o(i1,6),level_o(i1,5),bupdate6,sal_o(i1,6),V_1_lik(6),V_2_lik(6),V_3_lik(6),&
				      value_cont(value6,bupdate6), V_lik(6), &
				      edu(i1),p5,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp))

   
  IF(level_o(i1,6)>=4) THEN
   prob_wage_6  = 1.0_dp
  END IF
  

  IF(level_o(i1,6)==1) THEN
   prob_wage_6  = prob_wage(i2,level_o(i1,6),level_o(i1,5),bupdate6,sal_o(i1,6),V_1_lik(6),V_2_lik(6),V_3_lik(6),&
				      value_cont(value6,bupdate6), V_lik(6), &
				      edu(i1),p5,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1) ,REAL(tten-1,dp))

   ELSE IF(level_o(i1,6)==2) THEN
   prob_wage_6  = prob_wage(i2,level_o(i1,6),level_o(i1,5),bupdate6,sal_o(i1,6),V_1_lik(6),V_2_lik(6),V_3_lik(6),&
				      value_cont(value6,bupdate6), V_lik(6), &
				      edu(i1),p5,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1) ,REAL(tten-1,dp))

  ELSE IF(level_o(i1,6)==3) THEN
   prob_wage_6  = prob_wage(i2,level_o(i1,6),level_o(i1,5),bupdate6,sal_o(i1,6),V_1_lik(6),V_2_lik(6),V_3_lik(6),&
				      value_cont(value6,bupdate6), V_lik(6), &
				      edu(i1),p5,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp))

  END IF


  ELSE IF(sal_o(i1,6) == -2) THEN

   
   prob_wage_6  = 1.0_dp
  


 END IF



!-----------------------------------------------------------
!4. Accumulating prob of the SEQUENCE of observed ratings
!-----------------------------------------------------------
 

 
 i3 = 0 !loop over types
 !IF (perf_o(i1,4) .ne. -2  .and. perf_o(i1,5) .ne. -2)THEN
 IF (perf_o(i1,5) .ne. -2)THEN
  DO i3 = 1, 2 
   prob_perf_5(i3) = prob_joint_rating(i3,level_o(i1,5),level_o(i1,4),level_o(i1,6),perf_o(i1,4),perf_o(i1,3),&
                     perf_o(i1,5),p5,bupdate5,i2,tten-1)
  END DO
 ELSE
   DO i3 = 1,2 
    !p_success(hk,task,outcome, priortype)
    prob_perf_5(i3) = p_success(i3,level_o(i1,5),p5,i2)
    !prob_perf_5(i3) = 1.0_dp
  END DO
 END IF

i3 = 0
 DO i3 = 1,2
  IF(level_o(i1,5)>=3) THEN
   prob_perf_5(i3) = p_success(i3,level_o(i1,5),p5,i2)
   !prob_perf_5(i3) = 1.0_dp
  END IF
 END DO



!_______________________________________________________
! end per-period probability of level and rating, t=5
!_______________________________________________________


IF(itenure>=6) THEN
  !================
  DO p6_bis = 0,1
  !================

	!IF (level_o(i1,7) > level_o(i1,6)) THEN
	!  p6 = 1
    !ELSE
	  p6 = p6_bis
    !END IF
    


!______________________________________________________
!t=6: begin per-period probability of level and rating
!______________________________________________________
 !--------------------------------------------------------------------
 !1. Update after true performance, initialized at a particular prior
 !--------------------------------------------------------------------
  tten = 7
  bupdate7 = 0.0_dp
  bupdate7 = phi_bayes(prior(i1,i2),6, level_o(i1,1), p1, level_o(i1,2), p2, &
           level_o(i1,3), p3, level_o(i1,4), p4, level_o(i1,5), p5, level_o(i1,6), p6,&
		   level_o(i1,7), p7, level_o(i1,8), p8, level_o(i1,9), p9, i2, tten-1)
 

 !-------------------------------------------
 !2. Input into the prob of observed level
 !-------------------------------------------

  IF(level_o(i1,6) .ne. -2) THEN   

   leveldummy3 = level_o(i1,3) 
   leveldummy4 = level_o(i1,4) 
   leveldummy5 = level_o(i1,5)
   leveldummy6 = level_o(i1,6)  

     
	 IF (level_o(i1,3) >=3) THEN

	  leveldummy3 = 3

	 END IF

	 IF (level_o(i1,4) >=3) THEN

	  leveldummy4 = 3

	 END IF

	 IF (level_o(i1,5) >=3) THEN

	  leveldummy5 = 3

	 END IF


	 IF (level_o(i1,6) >=3) THEN

	  leveldummy6 = 3

	 END IF
 


   !V_0_lik(7)   = 0.0_dp
   CALL sub_interp1(phi_grid,V_0,bupdate7,phi_grid_size,z)  
   V_0_lik(7)   = z
   CALL sub_interp1(phi_grid,V_17(:,leveldummy3,leveldummy4,leveldummy5,leveldummy6),bupdate7,phi_grid_size,z)        
   V_1_lik(7)   = z
   CALL sub_interp1(phi_grid,V_27(:,leveldummy3,leveldummy4,leveldummy5,leveldummy6),bupdate7,phi_grid_size,z)
   V_2_lik(7)   = z
   CALL sub_interp1(phi_grid,V_37(:,leveldummy3,leveldummy4,leveldummy5,leveldummy6),bupdate7,phi_grid_size,z)
   V_3_lik(7)   = z
   
   V_lik(7)     = DMAX1(V_0_lik(7),V_1_lik(7),V_2_lik(7),V_3_lik(7))
  
  END IF

 !------------------------------------------------------------------
 !3. Accumulating probability of the SEQUENCE of observed levels
 !-------------------------------------------------------------------

  IF(level_o(i1,6)==-2) THEN

   prob_level_7 = 1.0_dp
 
  ELSE

  prob_level_7 = problevel(level_o(i1,7),V_lik(7),V_0_lik(7),V_1_lik(7),V_2_lik(7),V_3_lik(7), tten, level_o(i1,6) )

  END IF



 IF(sal_o(i1,7) .ne. -2 .and. level_o(i1,7) .ne. -2) THEN
  

  prob_wage_7  = prob_wage(i2,level_o(i1,7),level_o(i1,6),bupdate7,sal_o(i1,7),V_1_lik(7),V_2_lik(7),V_3_lik(7),&
				      value_cont(value7,bupdate7), V_lik(7), &
				      edu(i1),p6,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp) )

  
  IF(level_o(i1,7)==2) THEN
   prob_wage_7  = prob_wage(i2,level_o(i1,7),level_o(i1,6),bupdate7,sal_o(i1,7),V_1_lik(7),V_2_lik(7),V_3_lik(7),&
				      value_cont(value7,bupdate7), V_lik(7), &
				      edu(i1),p6,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp) )


  ELSE IF(level_o(i1,7)==3) THEN
   prob_wage_7  = prob_wage(i2,level_o(i1,7),level_o(i1,6),bupdate7,sal_o(i1,7),V_1_lik(7),V_2_lik(7),V_3_lik(7),&
				      value_cont(value7,bupdate7), V_lik(7), &
				      edu(i1),p6,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp) )

  END IF

 
  IF(level_o(i1,7)>=4) THEN
   prob_wage_7  = 1.0_dp
  END IF


  ELSE IF(sal_o(i1,7) == -2) THEN
   

   
   prob_wage_7  = 1.0_dp
  


 END IF



!----------------------------------------------------------
!4. Accumulating prob of the SEQUENCE of observed ratings
!----------------------------------------------------------

 i3 = 0 !loop over types
 !IF (perf_o(i1,5) .ne. -2 .and. perf_o(i1,6) .ne. -2)THEN
 IF (perf_o(i1,6) .ne. -2)THEN
  DO i3 = 1, 2 
   prob_perf_6(i3) = prob_joint_rating(i3,level_o(i1,6),level_o(i1,5),level_o(i1,7),perf_o(i1,5),perf_o(i1,4),&
                       perf_o(i1,6),p6,bupdate6,i2,tten-1)
  END DO
  
 ELSE
   DO i3 = 1,2 
    prob_perf_6(i3) = p_success(i3,level_o(i1,6),p6,i2)
    !prob_perf_6(i3) = 1.0_dp
  END DO
 END IF

i3 = 0
 DO i3 = 1,2
  IF(level_o(i1,6)>=3) THEN
    prob_perf_6(i3) = p_success(i3,level_o(i1,6),p6,i2)
   !prob_perf_6(i3) = 1.0_dp
  END IF
 END DO


!______________________________________________________
! end per-period probability of level and rating, t=6
!______________________________________________________


IF(itenure>=7) THEN	  
  
  !===============
  DO p7_bis = 0,1
  !===============

	!IF (level_o(i1,8) > level_o(i1,7)) THEN
	!  p7 = 1
    !ELSE
	  p7 = p7_bis
    !END IF


!_______________________________________________________
!t=7: begin per-period probability of level and rating
!_______________________________________________________

 !--------------------------------------------------------------------
 !1. Update after true performance, initialized at a particular prior
 !--------------------------------------------------------------------
  !IF (i1==13) THEN
  ! PRINT*, "level_o(i1,8)", level_o(13,8)
  !END IF
  
  tten = 8
  bupdate8 = 0.0_dp
  bupdate8 = phi_bayes(prior(i1,i2),7, level_o(i1,1), p1, level_o(i1,2), p2, &
           level_o(i1,3), p3, level_o(i1,4), p4, level_o(i1,5), p5, level_o(i1,6), p6,&
		   level_o(i1,7), p7, level_o(i1,8), p8, level_o(i1,9), p9, i2, tten-1)
 

 !-------------------------------------------
 !2. Input into the prob of observed level
 !-------------------------------------------


 !V_0_lik(8)   = 0.0_dp
 CALL sub_interp1(phi_grid,V_0,bupdate8,phi_grid_size,z)  
   V_0_lik(8)   = z
 CALL sub_interp1(phi_grid,V_18,bupdate8,phi_grid_size,z)        
 V_1_lik(8)   = z
 CALL sub_interp1(phi_grid,V_28,bupdate8,phi_grid_size,z)
 V_2_lik(8)   = z
 CALL sub_interp1(phi_grid,V_38,bupdate8,phi_grid_size,z)
 V_3_lik(8)   = z


   V_lik(8)     = DMAX1(V_0_lik(8),V_1_lik(8),V_2_lik(8),V_3_lik(8))
   !V_lik(8)     = value_cont(value,bupdate)       
   


 !------------------------------------------------------------------
 !3. Accumulating probability of the SEQUENCE of observed levels
 !------------------------------------------------------------------

  IF(level_o(i1,7)==-2) THEN

   prob_level_8 = 1.0_dp
 
  ELSE
 
   prob_level_8 = problevel(level_o(i1,8),V_lik(8),V_0_lik(8),V_1_lik(8),V_2_lik(8),V_3_lik(8),tten, level_o(i1,7) )

  END IF

 
 IF(sal_o(i1,8) .ne. -2  .and. level_o(i1,8) .ne. -2) THEN


  prob_wage_8  =     prob_wage(i2,level_o(i1,8),level_o(i1,7),bupdate8,sal_o(i1,8),V_1_lik(8),V_2_lik(8),V_3_lik(8),&
				      value_cont(value6,bupdate8), V_lik(8), &
				      edu(i1),p7,age(i1), year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp) )

 
  IF(level_o(i1,8)>=4) THEN
   prob_wage_8  = 1.0_dp
  END IF


  ELSE IF(sal_o(i1,8) == -2) THEN
 

   
   prob_wage_8  = 1.0_dp
  


 END IF



!----------------------------------------------------------
!4. Accumulating prob of the SEQUENCE of observed ratings
!----------------------------------------------------------
! IF(level_o(i1,6)==-2)THEN
!  PRINT*, 'For ind', i1, 'level 6 =-2'
!  PAUSE
! END IF
 
 i3 = 0 !loop over types
 !IF (perf_o(i1,6) .ne. -2 .and. perf_o(i1,7) .ne. -2)THEN
 IF (perf_o(i1,7) .ne. -2)THEN
  DO i3 = 1, 2 
   prob_perf_7(i3) = prob_joint_rating(i3,level_o(i1,7),level_o(i1,6),level_o(i1,8),perf_o(i1,6),&
                  perf_o(i1,5),perf_o(i1,7),p7,bupdate7,i2,tten-1)
  END DO
 ELSE
  DO i3 = 1,2 
    prob_perf_7(i3) = p_success(i3,level_o(i1,7),p7,i2)
    !prob_perf_7(i3) = 1.0_dp
  END DO
 END IF


i3 = 0
 DO i3 = 1,2
  IF(level_o(i1,7)>=3) THEN
   prob_perf_7(i3) = p_success(i3,level_o(i1,7),p7,i2)
   !prob_perf_7(i3) = 1.0_dp
  END IF
 END DO


!_______________________________________________________
! end per-period probability of level and rating, t=7
!_______________________________________________________


IF(itenure>=8) THEN	  
  !===============
  DO p8_bis = 0,1
  !===============	   

	!IF (level_o(i1,9) > level_o(i1,8)) THEN
	!  p8 = 1
    !ELSE
	  p8 = p8_bis
    !END IF


!______________________________________________________
!t=8: begin per-period probability of level and rating
!______________________________________________________

 !--------------------------------------------------------------------
 !1. Update after true performance, initialized at a particular prior
 !--------------------------------------------------------------------
  tten = 9
  bupdate9 = 0.0_dp
  bupdate9 = phi_bayes(prior(i1,i2),8, level_o(i1,1), p1, level_o(i1,2), p2, &
           level_o(i1,3), p3, level_o(i1,4), p4, level_o(i1,5), p5, level_o(i1,6), p6,&
		   level_o(i1,7), p7, level_o(i1,8), p8, level_o(i1,9), p9, i2, tten-1)
 

 !-------------------------------------------
 !2. Input into the prob of observed level
 !-------------------------------------------
 !V_0_lik(9)   = 0.0_dp
 CALL sub_interp1(phi_grid,V_0,bupdate9,phi_grid_size,z)  
 V_0_lik(9)   = z 
 CALL sub_interp1(phi_grid,V_18,bupdate9,phi_grid_size,z)        
 V_1_lik(9)   = z
 CALL sub_interp1(phi_grid,V_28,bupdate9,phi_grid_size,z)
 V_2_lik(9)   = z
 CALL sub_interp1(phi_grid,V_38,bupdate9,phi_grid_size,z)
 V_3_lik(9)   = z
  
  
   V_lik(9)     = DMAX1(V_0_lik(9),V_1_lik(9),V_2_lik(9),V_3_lik(9))
   !V_lik(9)     = value_cont(value,bupdate)       
   


 !------------------------------------------------------------------
 !3. Accumulating probability of the SEQUENCE of observed levels
 !------------------------------------------------------------------
 
  prob_level_9 = problevel(level_o(i1,9),V_lik(9),V_0_lik(9),V_1_lik(9),V_2_lik(9),V_3_lik(9),tten, level_o(i1,8))
 
 
 
 IF(sal_o(i1,9) .ne. -2) THEN


  prob_wage_9  = prob_wage(i2,level_o(i1,9),level_o(i1,8),bupdate9,sal_o(i1,9),V_1_lik(9),V_2_lik(9),V_3_lik(9),&
				      value_cont(value6,bupdate9), V_lik(9), &
				      edu(i1),p8,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp) )

 



  ELSE IF(sal_o(i1,9) == -2) THEN

   
   prob_wage_9  = 1.0_dp
  


 END IF



!----------------------------------------------------------
!4. Accumulating prob of the SEQUENCE of observed ratings
!----------------------------------------------------------
 
 i3 = 0 !loop over types

 !IF (perf_o(i1,7) .ne. -2 .and. perf_o(i1,8) .ne. -2)THEN

 IF (perf_o(i1,8) .ne. -2)THEN
  DO i3 = 1, 2 
   prob_perf_8(i3) = prob_joint_rating(i3,level_o(i1,8),level_o(i1,7),level_o(i1,9),perf_o(i1,7),perf_o(i1,6),&
                     perf_o(i1,8),p8,bupdate8,i2,tten-1)
  END DO
 ELSE
   DO i3 = 1,2 
      prob_perf_8(i3) = p_success(i3,level_o(i1,8),p8,i2)
     !prob_perf_8(i3) = 1.0_dp
  END DO
 END IF


 i3 = 0
 DO i3 = 1,2
  IF(level_o(i1,8)>=3) THEN
    prob_perf_8(i3) = p_success(i3,level_o(i1,8),p8,i2)
   !prob_perf_8(i3) = 1.0_dp
  END IF
 END DO




IF(i1==13)THEN
  PRINT*, 'prob_perf_8(1)', prob_perf_8(1)
  PRINT*, 'prob_perf_8(2)', prob_perf_8(2)
!! WRITE(3000,*) 'salary3', sal_o(i1,3)
!! WRITE(3000,*) 'prob_wage_3', prob_wage_3
END IF

!______________________________________________________
! end per-period probability of level and rating, t=8
!______________________________________________________



IF(itenure>=9) THEN	  
  !===============
  DO p9_bis = 0,1
  !===============


   !IF (level_o(i1,10) > level_o(i1,9)) THEN
   !	 p9 = 1
   !ELSE
     p9 = p9_bis
   !END IF


!______________________________________________________
!t=9: begin per-period probability of level and rating
!______________________________________________________

 !--------------------------------------------------------------------
 !1. Update after true performance, initialized at a particular prior
 !--------------------------------------------------------------------
  tten = 10
  bupdate10 = 0.0_dp
  bupdate10 = phi_bayes(prior(i1,i2),9, level_o(i1,1), p1, level_o(i1,2), p2, &
           level_o(i1,3), p3, level_o(i1,4), p4, level_o(i1,5), p5, level_o(i1,6), p6,&
		   level_o(i1,7), p7, level_o(i1,8), p8, level_o(i1,9), p9, i2, tten-1)
 

 !-------------------------------------------
 !2. Input into the prob of observed level
 !-------------------------------------------
  

 !V_0_lik(10)   = 0.0_dp
 CALL sub_interp1(phi_grid,V_0,bupdate10,phi_grid_size,z)  
 V_0_lik(10)   = z
 CALL sub_interp1(phi_grid,V_18,bupdate10,phi_grid_size,z)        
 V_1_lik(10)   = z
 CALL sub_interp1(phi_grid,V_28,bupdate10,phi_grid_size,z)
 V_2_lik(10)   = z
 CALL sub_interp1(phi_grid,V_38,bupdate10,phi_grid_size,z)
 V_3_lik(10)   = z
   
   V_lik(10)     = DMAX1(V_0_lik(10),V_1_lik(10),V_2_lik(10),V_3_lik(10))
   !V_lik(10)     = value_cont(value,bupdate)       
   


 !------------------------------------------------------------------
 !3. Accumulating probability of the SEQUENCE of observed levels
 !-------------------------------------------------------------------

 prob_level_10 = problevel(level_o(i1,10),V_lik(10),V_0_lik(10),V_1_lik(10),V_2_lik(10),V_3_lik(10),tten, level_o(i1,9))



 IF(sal_o(i1,10) .ne. -2) THEN

  prob_wage_10  = prob_wage(i2,level_o(i1,10),level_o(i1,9),bupdate10,sal_o(i1,10),V_1_lik(10),V_2_lik(10),V_3_lik(10),&
				      value_cont(value6,bupdate10), V_lik(10), &
				      edu(i1),p8,age(i1),year0(i1), year1(i1), year2(i1), year3(i1), year4(i1), &
					  year5(i1), year6(i1), year7(i1), year8(i1), year9(i1),REAL(tten-1,dp))

 

  ELSE IF(sal_o(i1,10) == -2) THEN
   
  
   
   prob_wage_10  = 1.0_dp
  


 END IF



!-----------------------------------------------------------
!4. Accumulating prob of the SEQUENCE of observed ratings
!-----------------------------------------------------------
 
! IF(level_o(i1,8)==-2)THEN
!  PRINT*, 'For ind', i1, 'level 8 =-2'
!  PAUSE
! END IF


i3 = 0 !loop over types

!IF (perf_o(i1,8) .ne. -2  .and. perf_o(i1,9) .ne. -2)THEN

IF (perf_o(i1,9) .ne. -2)THEN
  DO i3 = 1, 2 
    prob_perf_9(i3) = prob_joint_rating(i3,level_o(i1,9),level_o(i1,8),level_o(i1,10),perf_o(i1,8),perf_o(i1,7),& 
	                     perf_o(i1,9),p9,bupdate9,i2,tten-1)
  END DO
ELSE
   DO i3 = 1,2 
    prob_perf_9(i3) = p_success(i3,level_o(i1,9),p9,i2)
    !prob_perf_9(i3) = 1.0_dp
  END DO
END IF 

 i3 = 0
 DO i3 = 1,2
  IF(level_o(i1,9)>=3) THEN
   prob_perf_9(i3) = p_success(i3,level_o(i1,9),p9,i2)
   !prob_perf_9(i3) = 1.0_dp
  END IF
 END DO




!______________________________________________________
! end per-period probability of level and rating, t=9
!______________________________________________________
IF(itenure>=10) THEN
    
  !================
   DO p10_bis = 0,1
  !================

   !IF (level_o(i1,11) > level_o(i1,10)) THEN
   !	 p10 = 1
   !ELSE
     p10 = p10_bis
   !END IF
  


!______________________________________________________
!t=10: begin per-period probability of level and rating
!______________________________________________________          

!------------------------------------------------------------
!1. Accumulating prob of the SEQUENCE of observed ratings
!------------------------------------------------------------

 IF(level_o(i1,9)==-2)THEN
  PRINT*, 'For ind', i1, 'level 9 =-2'
  PAUSE
 END IF


i3=0  !loop over types

!IF (perf_o(i1,9) .ne. -2 .and. perf_o(i1,10) .ne. -2)THEN

IF (perf_o(i1,10) .ne. -2)THEN

 DO i3 = 1, 2 
    prob_perf_10(i3) = prob_joint_rating(i3,level_o(i1,10),level_o(i1,9),-2,perf_o(i1,9),perf_o(i1,8),perf_o(i1,10),&
	                   p10,bupdate10,i2,tten-1)
 END DO
ELSE
   DO i3 = 1,2 
    prob_perf_10(i3) = p_success(i3,level_o(i1,10),p10,i2)
	!prob_perf_10(i3) = 1.0_dp
  END DO
END IF


i3 = 0
 DO i3 = 1,2
  IF(level_o(i1,10)>=3) THEN
    prob_perf_10(i3) = p_success(i3,level_o(i1,10),p10,i2)
	!prob_perf_10(i3) = 1.0_dp
   
  END IF
 END DO
  


!_____________________________________________
! end per-period probability of rating, t=10
!_____________________________________________
  


!-----------------------------------
!a. Observed performance until t=10
!-----------------------------------
i3 = 0
 DO i3 = 1, 2  
  
  like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3) + prob_level_1*prob_level_2*prob_level_3&
             *prob_level_4*prob_level_5*prob_level_6*prob_level_7*prob_level_8*prob_level_9&
	         *prob_level_10*prob_perf_1(i3)*prob_perf_2(i3)*prob_perf_3(i3)*prob_perf_4(i3)&
             *prob_perf_5(i3)*prob_perf_6(i3)*prob_perf_7(i3)*prob_perf_8(i3)*prob_perf_9(i3)&
             *prob_perf_10(i3)*prob_wage_1*prob_wage_2*prob_wage_3*prob_wage_4*prob_wage_5&
			 *prob_wage_6*prob_wage_7*prob_wage_8*prob_wage_9*prob_wage_10

 IF (like_prior_type(i1,i2,i3) < tiny35) THEN
  like_prior_type(i1,i2,i3) = tiny35
 
 ELSE IF (like_prior_type(i1,i2,i3) >= tiny35 ) THEN
   like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3)
 
  !WRITE(3000,*) "Like_prior_type > 1 for ind", i1, "- prior", i2, "- type", i3
 END IF

  			 
 END DO


!_________________
END DO ! for p10
!_________________
END IF !loop on itenure >=10 




!-----------------------------------
!b. observed performance until t=9
!-----------------------------------
!______________________
IF(itenure <10) THEN
!______________________

i3 = 0
 DO i3 = 1, 2  
  
  like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3) + prob_level_1*prob_level_2*prob_level_3&
             *prob_level_4*prob_level_5*prob_level_6*prob_level_7*prob_level_8*prob_level_9&
	         *prob_level_10*prob_perf_1(i3)*prob_perf_2(i3)*prob_perf_3(i3)*prob_perf_4(i3)&
             *prob_perf_5(i3)*prob_perf_6(i3)*prob_perf_7(i3)*prob_perf_8(i3)*prob_perf_9(i3)&
			 *prob_wage_1*prob_wage_2*prob_wage_3*prob_wage_4*prob_wage_5&
			 *prob_wage_6*prob_wage_7*prob_wage_8*prob_wage_9*prob_wage_10

             

 IF (like_prior_type(i1,i2,i3) < tiny35) THEN
  like_prior_type(i1,i2,i3) = tiny35
 
 ELSE IF (like_prior_type(i1,i2,i3) >= tiny35 ) THEN
   like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3)

 END IF

  			 
 END DO

!__________________________
END IF ! for itenure < 10
!__________________________
!_________________________
END DO ! for p9
!____________________________
END IF !loop on itenure >=9
!____________________________

!-----------------------------------
!c. observed performance until t=8
!-----------------------------------
!___________________
IF(itenure <9) THEN
!___________________
i3 = 0
 DO i3 = 1, 2  
  
  like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3) + prob_level_1*prob_level_2*prob_level_3&
             *prob_level_4*prob_level_5*prob_level_6*prob_level_7*prob_level_8*prob_level_9&
	         *prob_perf_1(i3)*prob_perf_2(i3)*prob_perf_3(i3)*prob_perf_4(i3)&
             *prob_perf_5(i3)*prob_perf_6(i3)*prob_perf_7(i3)*prob_perf_8(i3)&
			 *prob_wage_1*prob_wage_2*prob_wage_3*prob_wage_4*prob_wage_5&
			 *prob_wage_6*prob_wage_7*prob_wage_8*prob_wage_9

             

 IF (like_prior_type(i1,i2,i3) < tiny35) THEN
  like_prior_type(i1,i2,i3) = tiny35
 
 ELSE IF (like_prior_type(i1,i2,i3) >= tiny35 ) THEN
   like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3)
 
 
 END IF

  			 
 END DO

!_________________________
END IF ! for itenure <9
!_________________________
!_______________________
END DO ! for p8
!_______________________
END IF !loop on itenure >=8



!-----------------------------------
!d. observed performance until t=7
!-----------------------------------
!____________________
IF(itenure <8) THEN
!____________________

i3 = 0
 DO i3 = 1, 2  
  
  like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3) + prob_level_1*prob_level_2*prob_level_3&
             *prob_level_4*prob_level_5*prob_level_6*prob_level_7&
	         *prob_perf_1(i3)*prob_perf_2(i3)*prob_perf_3(i3)*prob_perf_4(i3)&
             *prob_perf_5(i3)*prob_perf_6(i3)*prob_perf_7(i3)&
			 *prob_wage_1*prob_wage_2*prob_wage_3*prob_wage_4*prob_wage_5&
			 *prob_wage_6*prob_wage_7

             

 IF (like_prior_type(i1,i2,i3) < tiny35) THEN
  like_prior_type(i1,i2,i3) = tiny35
 
 ELSE IF (like_prior_type(i1,i2,i3) >= tiny35 ) THEN
   like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3)
 
 
 END IF

  			 
 END DO

!______________________
END IF ! for itenure <8
!______________________
!_______________________
END DO ! for p7
!_______________________
END IF !loop on itenure >=7


!-----------------------------------
!e. observed performance until t=6
!-----------------------------------
!___________________
IF(itenure <7) THEN
!___________________
!IF(i1 == 868) THEN
! PRINT*, 'ind', i1
!END IF


i3 = 0
 DO i3 = 1, 2  

  !PRINT*, 'sum_like_prior', like_prior_type(i1,i2,i3) 
  
  like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3) + prob_level_1*prob_level_2*prob_level_3&
             *prob_level_4*prob_level_5*prob_level_6&
	         *prob_perf_1(i3)*prob_perf_2(i3)*prob_perf_3(i3)*prob_perf_4(i3)&
             *prob_perf_5(i3)*prob_perf_6(i3)*prob_wage_1*prob_wage_2*prob_wage_3*prob_wage_4*prob_wage_5&
			 *prob_wage_6
           

 IF (like_prior_type(i1,i2,i3) < tiny35) THEN
  like_prior_type(i1,i2,i3) = tiny35
 
 ELSE IF (like_prior_type(i1,i2,i3) >= tiny35 .and. like_prior_type(i1,i2,i3) <= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3)
 
 ELSE IF (like_prior_type(i1,i2,i3) >= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = 1.0d+15
   
  
 
 END IF
 !PRINT*, 'corrected like_prior', like_prior_type(i1,i2,i3) 
 !PRINT*, ''
  			 
 END DO

!___________________
END IF ! itenure <7
!___________________
!____________________
END DO ! for p6
!____________________
END IF !loop on itenure >=6



!-----------------------------------
!f. observed performance until t=5
!-----------------------------------
!______________________
IF(itenure < 6) THEN
!______________________

i3 = 0
 DO i3 = 1, 2  
  
  like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3) + prob_level_1*prob_level_2*prob_level_3&
             *prob_level_4*prob_level_5&
	         *prob_perf_1(i3)*prob_perf_2(i3)*prob_perf_3(i3)*prob_perf_4(i3)&
             *prob_perf_5(i3)*prob_wage_1*prob_wage_2*prob_wage_3*prob_wage_4*prob_wage_5
             

 IF (like_prior_type(i1,i2,i3) < tiny35) THEN
  like_prior_type(i1,i2,i3) = tiny35
 
 ELSE IF (like_prior_type(i1,i2,i3) >= tiny35 .and. like_prior_type(i1,i2,i3) <= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3)
 
 ELSE IF (like_prior_type(i1,i2,i3) >= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = 1.0d+15
   !PRINT*, like_prior_type(i1,i2,i3) 
 
 END IF

  			 
 END DO

!_________________________
END IF ! for itenure <6
!_________________________
!____________________
END DO ! for p5
!____________________
END IF !loop on itenure >=5


!-----------------------------------
!g. observed performance until t=4
!-----------------------------------
!______________________
IF(itenure < 5) THEN
!______________________

i3 = 0
 DO i3 = 1, 2  
  
  like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3) + prob_level_1*prob_level_2*prob_level_3&
             *prob_level_4*prob_perf_1(i3)*prob_perf_2(i3)*prob_perf_3(i3)*prob_perf_4(i3)&
			 *prob_wage_1*prob_wage_2*prob_wage_3*prob_wage_4
             

 IF (like_prior_type(i1,i2,i3) < tiny35) THEN
  like_prior_type(i1,i2,i3) = tiny35
 
 ELSE IF (like_prior_type(i1,i2,i3) >= tiny35 .and. like_prior_type(i1,i2,i3) <= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3)
 
 ELSE IF (like_prior_type(i1,i2,i3) >= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = 1.0d+15
   !PRINT*, like_prior_type(i1,i2,i3) 

 END IF

  			 
 END DO

!________________________
END IF ! for itenure <5
!________________________
!____________________
END DO ! for p4
!____________________
END IF !loop on itenure >=4



!-----------------------------------
!h. observed performance until t=3
!-----------------------------------
!_____________________
IF(itenure <4) THEN
!_____________________

i3 = 0
 DO i3 = 1, 2  
  
  like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3) + prob_level_1*prob_level_2*prob_level_3&
             *prob_perf_1(i3)*prob_perf_2(i3)*prob_perf_3(i3)*prob_wage_1*prob_wage_2&
			 *prob_wage_3
            

 IF (like_prior_type(i1,i2,i3) < tiny35) THEN
  like_prior_type(i1,i2,i3) = tiny35

 ELSE IF (like_prior_type(i1,i2,i3) >= tiny35 .and. like_prior_type(i1,i2,i3) <= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3)
 
 ELSE IF (like_prior_type(i1,i2,i3) >= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = 1.0d+15
   !PRINT*, like_prior_type(i1,i2,i3) 
 
 END IF

  			 
 END DO


!___________________
END IF !itenure <4
!___________________
!__________________
END DO ! for p3
!__________________
END IF !loop on itenure >=3

!-----------------------------------
!i. observed performance until t=2
!-----------------------------------
!____________________
IF(itenure <3) THEN
!____________________
i3 = 0
 DO i3 = 1, 2  
  
  like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3) + prob_level_1*prob_level_2&
             *prob_perf_1(i3)*prob_perf_2(i3)*prob_wage_1*prob_wage_2

             

 IF (like_prior_type(i1,i2,i3) < tiny35) THEN
  like_prior_type(i1,i2,i3) = tiny35
 
ELSE IF (like_prior_type(i1,i2,i3) >= tiny35 .and. like_prior_type(i1,i2,i3) <= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3)
 
 ELSE IF (like_prior_type(i1,i2,i3) >= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = 1.0d+15
   !PRINT*, like_prior_type(i1,i2,i3) 
 

 END IF
 END DO

!________________________
END IF ! for itenure <3
!________________________
!_________________
 END DO ! for p2
!_________________
END IF !loop on itenure >=2



!*********************************************************************
!(7) Likelihood contribution by IND (i1), PRIOR (i2), TYPE (i3) 
!*********************************************************************
!___________________
IF(itenure <2)THEN
!___________________

i3 = 0
 DO i3 = 1, 2  
  
  !like_prior_type(i1,i2,i3) = prob_level_1*prob_wage_1
  like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3) + prob_level_1&
                              *prob_perf_1(i3)*prob_wage_1
             

 IF (like_prior_type(i1,i2,i3) < tiny35) THEN
  like_prior_type(i1,i2,i3) = tiny35
 
 ELSE IF (like_prior_type(i1,i2,i3) >= tiny35 .and. like_prior_type(i1,i2,i3) <= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = like_prior_type(i1,i2,i3)
 
 ELSE IF (like_prior_type(i1,i2,i3) >= 1.0d+15) THEN
   like_prior_type(i1,i2,i3) = 1.0d+15
   !PRINT*, like_prior_type(i1,i2,i3) 

 END IF

  			 
 END DO



!________________________
END IF ! for itenure <2
!________________________
!_______________
END DO !for p1
!_______________
!===========================
END IF !loop on itenure >=1
!===========================
!============================
END DO ! i2: prior
END DO ! i1: individual
!============================


DEALLOCATE(year0, year1, year2, year3,year4, year5, year6, year7, year8,year9)
DEALLOCATE(tenure_2_L1,tenure_2_L2)
DEALLOCATE(tenure_3_L1,tenure_3_L2,tenure_3_L3)
DEALLOCATE(tenure_4_L1,tenure_4_L2,tenure_4_L3)
DEALLOCATE(tenure_5_L1,tenure_5_L2,tenure_5_L3)
DEALLOCATE(tenure_6_L1,tenure_6_L2,tenure_6_L3)
DEALLOCATE(tenure_7_L1,tenure_7_L2,tenure_7_L3)



WRITE(3000,*) ""

!PRINT*, "End of the loop on inds and priors"

i1 = 0
WRITE(3000,*) "Prior- & Type-Conditional Like for the first 5 inds (first prior):"
WRITE(3000,*) ""
 DO i1 = 1, 5
  WRITE(3000,*)  "H Type",  like_prior_type(i1,1,2), "of ind", i1 
  WRITE(3000,*)  "L Type",  like_prior_type(i1,1,1), "of ind", i1
  WRITE(3000,*) ""
 END DO

i1 = 0
WRITE(3000,*) "Prior- & Type-Conditional Like for the last 10 inds (first prior):"
WRITE(3000,*) ""
 DO i1 = 1411, 1416
  WRITE(3000,*)  "H Type",  like_prior_type(i1,1,2), "of ind", i1 
  WRITE(3000,*)  "L Type",  like_prior_type(i1,1,1), "of ind", i1
  WRITE(3000,*) ""
 END DO



!*****************************************************************************
!(8) Probability of INDIVIDUAL EMPLOYMENT HISTORIES conditional on the PRIOR
!*****************************************************************************
PRINT*, "2. Mixture over types of individual like"

!PRINT*, "Allocate probability of prior-dependent individual histories"
ALLOCATE(like_prior(totsample,prior_sim_dimension))


 i1 = 0
 i2 = 0
!---------------------------------------------------------------------------------------------------------------------------
!mixture over THEORETICAL TYPES - like_prior_type(individual,prior, type) e.g.
!like_prior(i1,1) = (prior(i1)*like_prior_type(i1,1,2)+(1.0_dp - prior(i1))*like_prior_type(i1,1,1))**(REAL(totnum(i1),dp))
!---------------------------------------------------------------------------------------------------------------------------
 DO i1 = 1, totsample 
  DO i2 = 1, prior_sim_dimension
   like_prior(i1,i2) = prior(i1,i2)*like_prior_type(i1,i2,2)+(1.0_dp - prior(i1,i2))*like_prior_type(i1,i2,1)
  END DO
 END DO



DEALLOCATE(prior)
DEALLOCATE(like_prior_type)

!PRINT*, "End of the mixture over types of the individual like"



!************************************************************
!(9) Integrate out skill types (like_prior(INDIVIDUAL,PRIOR))
!************************************************************
PRINT*, "3. Integrate out the prior"

ALLOCATE(wtd_like_ind(totsample))


i1 = 0
i2 = 0

wtd_like_ind = 0.0_dp


pr1 = p_1
pr2 = p_2
pr3 = p_3
pr4 = 1.0_dp - p_1 - p_2 - p_3



PRINT*, 'pr1', pr1
PRINT*, 'pr2', pr2
PRINT*, 'pr3', pr3
PRINT*, 'pr4', pr4
PRINT*, 'tot', pr1 + pr2 + pr3 + pr4



DO i1 = 1, totsample


  wtd_like_ind(i1) = (like_prior(i1,1)*pr1 + like_prior(i1,2)*pr2 + like_prior(i1,3)*pr3 + like_prior(i1,4)*pr4)**REAL(totnum(i1),dp)
  

END DO 



!**************************************************************
!(10) Compute the negative log-likelihood
!**************************************************************
PRINT*, "4. Negative loglike"
!===========================
ALLOCATE(neglike(totsample))
!===========================
minlikeloc = MINLOC(wtd_like_ind)
minlike = wtd_like_ind(minlikeloc(1))

!OPEN(unit=5000, file = 'loglike_file1.txt', position='rewind')
i1 = 0
DO i1 = 1, totsample
 
 !IF(i1 ==34) THEN
 ! PRINT*, '34'
 !END IF

   IF(wtd_like_ind(i1) > 1.0d-35 ) THEN
   neglike(i1) = -DLOG(wtd_like_ind(i1))
   loglike(i1) = DLOG(wtd_like_ind(i1))
!   WRITE(5000,*) loglike(i1)
    
  ELSE 
   neglike(i1) = -DLOG(1.0d-35 )
   loglike(i1) = DLOG(1.0d-35 )
!   WRITE(5000,*) loglike(i1)

  END IF  
  !PRINT*, 'Ind', i1, 'neglike', neglike(i1)
END DO


neglike(34)   = 0.0_dp
neglike(1303) = 0.0_dp
neglike(1417) = 0.0_dp
neglike(1421) = 0.0_dp



i1 = 0
DO i1 = 1, totsample
 IF(neglike(i1)>= 10.0_dp) THEN
 WRITE(3000,*) 'Ind', i1, 'neglike', neglike(i1)
 END IF
END DO
WRITE(3000,*) ''



OPEN(unit=5001, file = 'neglike_file1.txt', position='rewind')
i1 = 0
DO i1 = 1, totsample

 WRITE(5001,*) neglike(i1)

END DO

CLOSE(unit=5001)


OPEN(unit=5000, file = 'loglike_file1.txt', position='rewind')
i1 = 0
DO i1 = 1, totsample

 WRITE(5000,*) -neglike(i1)

END DO

CLOSE(unit=5000)


mineglikeloc = MINLOC(neglike)
mineglike = neglike(mineglikeloc(1))



DEALLOCATE(wtd_like_ind)
DEALLOCATE(like_prior)
DEALLOCATE(totnum)
DEALLOCATE(edu, age, year)
DEALLOCATE(level_o, sal_o, perf_o)



!*********************************************************************
!(11) Calculate objective function for the minimization algorithm
!*********************************************************************

F(1) = SUM(neglike)

PRINT*, "Final like (sum of ind neglike)", F(1)

DEALLOCATE(neglike)


END SUBROUTINE likelihood_function
